交易失败
来源:stackoverflow
时间:2024-04-18 18:06:34 126浏览 收藏
本篇文章给大家分享《交易失败》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
问题内容
我正在用 go 编写一个带有 mariadb 连接器的应用程序,但收到以下错误。当我在命令行上手动将输出粘贴到 mysql 时,它工作得很好。我不确定为什么它会在 go 应用程序中失败。
begin; insert into content (content) values ('testing'); set @last_id_in_table1 = last_insert_id(); insert into article_meta (article_title, article_author, description) values ('title', 'bob', ''); set @last_id_in_table2 = last_insert_id(); insert into articles (content_id, article_id) values (@last_id_in_table1, @last_id_in_table2); commit; 2021/02/07 03:47:54 error 1064: you have an error in your sql syntax; check the manual that corresponds to your mariadb server version for the right syntax to use near 'insert into content (content) values ('testing'); set @last_id_in_table1 ...' at line 2 exit status 1
mariadb [golang]> begin; query ok, 0 rows affected (0.000 sec) mariadb [golang]> insert into content (content) values ('testing'); query ok, 1 row affected (0.001 sec) mariadb [golang]> set @last_id_in_table1 = last_insert_id(); query ok, 0 rows affected (0.000 sec) mariadb [golang]> insert into article_meta -> (article_title, article_author, description) -> values ('title', 'bob', ''); query ok, 1 row affected (0.056 sec) mariadb [golang]> set @last_id_in_table2 = last_insert_id(); query ok, 0 rows affected (0.000 sec) mariadb [golang]> insert into articles -> (content_id, article_id) -> values (@last_id_in_table1, @last_id_in_table2); query ok, 1 row affected (0.034 sec) mariadb [golang]> commit; query ok, 0 rows affected (0.033 sec)
所有其他 mysql 似乎都可以在 go 应用程序中正常工作。我可以毫无问题地执行查询和插入/更新。我不知道为什么这个特定查询失败。任何帮助将不胜感激。
这是有问题的具体代码片段:
query := fmt.Sprintf(` BEGIN; INSERT INTO content (content) VALUES ('%s'); SET @last_id_in_table1 = LAST_INSERT_ID(); INSERT INTO article_meta (article_title, article_author, description) VALUES ('%s', '%s', '%s'); SET @last_id_in_table2 = LAST_INSERT_ID(); INSERT INTO articles (content_id, article_id) VALUES (@last_id_in_table1, @last_id_in_table2); COMMIT; `, article.Content, article.Title, article.Auth, article.Description) fmt.Println(query) rows, err2 := db.Query(query) if err2 != nil { log.Fatal(err2) }
解决方案
您应该使用 tx, err := db.begin()
启动事务,然后使用 tx.query/queryrow/exec
调用各个语句,然后,如果成功,请使用 tx.commit()
提交该事务,或者如果不成功则 tx.rollback()
中止。
另请注意,将 %s
与 fmt.printf
一起使用会使您的代码容易受到 sql 注入的攻击。您应该使用您正在使用的 dbms 和/或驱动程序支持的“参数占位符”。就像 mysql 中的 ?
或 postgres 中的 $n
一样。
类似以下内容应该有效:
func insertArticle(a *Article) (err error) { tx, err := db.Begin() if err != nil { return err } defer func() { if err != nil { tx.Rollback() } }() res, err := tx.Exec("INSERT INTO content (content) VALUES (?)", a.Content) if err != nil { return err } contentId, err := res.LastInsertId() if err != nil { return err } res, err = tx.Exec("INSERT INTO article_meta (article_title, article_author, description) VALUES (?, ?, ?)", a.Title, a.Auth, a.Description) if err != nil { return err } articleId, err := res.LastInsertId() if err != nil { return err } _, err = tx.Exec("INSERT INTO articles (content_id, article_id) VALUES (?, ?)", contentId, articleId) if err != nil { return err } return tx.Commit() }
今天带大家了解了的相关知识,希望对你有所帮助;关于Golang的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~
声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
最新阅读
更多>
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
课程推荐
更多>
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习