DBExpress和mysql最后插入id
DBExpress and mysql last insert id
有几个关于如何从MySQL获取last_insert_id和相关问题的帖子。我发现这一职位有关的DBExpress: delphi dxExpress MySQL:无效的LAST_INSERT_ID值
然而,遵循这篇文章并不能帮助我使用下面的代码将注释插入到注释表中。
TSQLQuery* tq = new TSQLQuery(NULL);
tq->SQLConnection = atdbDM->SQLConnection1;
stringstream q;
q <<"INSERT INTO note (created_by, note) VALUES ("<<1234<<", '<none>');";
q << "SELECT LAST_INSERT_ID() AS lastNoteID;";
tq->SQL->Add(q.str().c_str());
tq->Open();
int noteID = tq->FieldByName("lastNoteID")->AsInteger;
tq->Close();
当Open()执行时,我得到一个异常:
'You have an error in your SQL syntax; Check the manual that correposnds to your MySQL server version for the right syntax to use near 'SELECT LAST_INSERT_ID... at line1'
如果我先执行insert语句,然后分别执行SELECT语句,我不会得到任何错误,但是返回的ID总是0。有人知道需要改变什么吗?我正在使用c++ Builder XE3
我发现,为了防止创建多个会话,导致last_insertid函数始终返回0,可以将连接AUTOCLONE参数设置为false。那么最后一个插入id函数工作正常。即:
MySQLConnection->Connected = true;
MySQLConnection->AutoClone = false; //<- makes last_insert_id work as expected!
相关文章:
- 使用C++库在Android项目中修改gradle中的cmake参数,用于插入指令的测试
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 预处理器:插入结构名称中的前一个行号
- 在未初始化映射的情况下,将值插入到映射的映射中
- 如何在c++中只将键插入到bimap的一侧
- 如何将结构插入到集合中并打印集合的成员
- C++json插入数组
- Visual Studio 2019:插入多个C++风格的单行注释
- nlohmann-json将一个数组插入到另一个数组中
- 有效地使用std::unordered_map来插入或增加键的值
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 正在插入动态数组
- 插入或删除时获取usb的dos_name
- 从函数角度看ID到文件路径的内部与外部映射
- 如何使用 QSqlQuery 和 SQLite 从新插入的行中获取 ID
- Oracle使用OCCI和C++获取最后插入的行id
- 如何在另一个表中使用表的上次插入id
- 如何获取插入菜单项时要使用的下一个命令 ID
- C++MySql获取最后插入的id
- DBExpress和mysql最后插入id