登录
首页 >  Golang >  Go问答

交易失败

来源: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() 中止。

另请注意,将 %sfmt.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删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>