gpt4 book ai didi

MySQL异常: Execute SQL Transaction

转载 作者:行者123 更新时间:2023-11-29 08:43:08 26 4
gpt4 key购买 nike

我正在尝试向 MySQL 提交一个 sql 事务,但我遇到了 MySQLSyntaxErrorException

我使用的代码是:

implicit connection => 
SQL("""
start transaction;
insert into projects(id_user, name, description) values({idUser}, {name}, {description});
set @last_id = last_insert_id();
insert into assigned(id_user, id_project) values({idUser}, @last_id);
commit;
""")
.on('idUser -> idUser,
'name -> project.name,
'description -> project.description
).execute()

我得到的异常:

[MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'insert into projects(id_user, name, description) values(1, 'First inserted proje' at line 1]  

我开始认为我根本无法使用 Anorm 执行此类语句。

最佳答案

您不能以这种方式使用事务。您必须了解 anorm 只是现有 jdbc 库的包装器。默认情况下,当使用withConnectionSQL时:

DB.withConnection { conn => 
SQL("...
}

您的查询已使用 PreparedStatement 进行转换。这意味着 ; 字符导致错误。

因此,如果你想使用事务,你必须使用 anorm's mecanism为了那个原因。

DB.withTransaction { conn =>
SQL("...
}

关于MySQL异常: Execute SQL Transaction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13212900/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com