使用准备好的语句后 SELECT LAST_INSERT_ID() 返回 0
我正在使用 MySQL 和准备好的语句来插入 BLOB 记录(jpeg 图像)。执行准备好的语句后,我发出一个 SELECT LAST_INSERT_ID() 并返回 0。
在我的代码中,我在执行命令后放置了一个断点,并在 MySQL 命令(监视器)窗口中发出 < code>SELECT LAST_INSERT_ID() 并返回零。
在 MySQL 命令(监视器)窗口中,我发出一条 SQL 语句来选择所有 ID,最后(唯一)插入的 ID 是 1(一)。
我正在使用:
- 服务器版本:5.1.46-community MySQL 社区服务器 (GPL)
- Visual Studio 2008,版本 9。
- MySQL 连接器 C++ 1.0.5
我的表描述:
mysql> describe picture_image_data;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| ID_Image_Data | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Image_Data | mediumblob | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
2 rows in set (0.19 sec)
使用 MySQL 监视器的结果:
mysql> select ID_Image_Data
-> from picture_image_data;
+---------------+
| ID_Image_Data |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
准备好的语句:
INSERT INTO Picture_Image_Data
(ID_Image_Data, Image_Data)
VALUES
(?,
?);
上面的结果显示 ID_Image_Data 字段为 1,但 LAST_INSERT_ID 为零。该表是在执行该语句之前创建的,因此这是表中的唯一记录。
编辑:
我将 ID 字段设置为零,并将下一个字段设置为 C++ std::istream *
。根据 MySQL 手册页 LAST_INSERT_ID()
:
如果将行的 AUTO_INCREMENT 列设置为非“魔法”值(也就是说,非 NULL 且非 0 的值)。
LAST_INSERT_ID() 应该返回 1,因为准备好的语句中的 ID 值为 0。
我需要准备一个准备好的语句吗?用于获取 LAST_INSERT_ID 的语句?
{搜索网络显示使用自定义 MySQL API,但使用了 PHP,并且进一步的评论表示它需要另一个连接)。
I am using MySQL and a prepared statement to insert a BLOB
record (jpeg image). After executing the prepared statement, I issue a SELECT LAST_INSERT_ID()
and it returns 0.
In my code I put a breakpoint after the execution command and in a MySQL command (monitor) window, I issue the SELECT LAST_INSERT_ID()
and it returns zero.
In the MySQL command (monitor) window, I issue an SQL statement to select all the IDs and the last (only) inserted ID is 1 (one).
I am using:
- Server version: 5.1.46-community
MySQL Community Server (GPL) - Visual Studio 2008, version 9.
- MySQL Connector C++ 1.0.5
My table description:
mysql> describe picture_image_data;
+---------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+----------------+
| ID_Image_Data | int(10) unsigned | NO | PRI | NULL | auto_increment |
| Image_Data | mediumblob | YES | | NULL | |
+---------------+------------------+------+-----+---------+----------------+
2 rows in set (0.19 sec)
Results using MySQL monitor:
mysql> select ID_Image_Data
-> from picture_image_data;
+---------------+
| ID_Image_Data |
+---------------+
| 1 |
+---------------+
1 row in set (0.00 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 0 |
+------------------+
1 row in set (0.00 sec)
The Prepared statement:
INSERT INTO Picture_Image_Data
(ID_Image_Data, Image_Data)
VALUES
(?,
?);
The results above show that the ID_Image_Data field is one, but the LAST_INSERT_ID is zero. The table is created before this statement is executed, so this is the only record in the table.
Edit:
I am setting the ID field to zero and the next field to a C++ std::istream *
. According to the MySQL Manual Page for LAST_INSERT_ID()
:
The value of LAST_INSERT_ID()
is not changed if you set the AUTO_INCREMENT column of a row to a non-“magic” value (that is, a value that is not NULL and not 0).
The LAST_INSERT_ID() should return 1 since the ID value in the prepared statement is 0.
Do I need to make a prepared statement for fetching LAST_INSERT_ID?
{Searching the web showed to use a custom MySQL API, but that used PHP and futher comments said it needs another connection).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
LAST_INSERT_ID 仅适用于在 auto_increment 字段上创建的自动生成的主键。在您的情况下,您似乎显式提供了 id,因此未设置最后插入 id。
这是显式的:
这是隐式的:
LAST_INSERT_ID would only work for auto generated primary key, that was created on auto_increment field. In your case, it looks like you are supplying the id explicitly, so last insert id is not set.
This is explicit:
This is implicit:
看起来问题出在 MySQL C++ 连接器上。
LAST_INSERT_ID()
在以下情况下返回 0:ID 字段为空,显式
插入。
LAST_INSERT_ID()
在以下情况下返回 0:未指定 ID 字段,
隐式插入。
我尝试从命令行(监视器)插入 BLOB(JPEG 图像),并且它有效:
目前,解决方法是使用 SQL 语句通过准备好的语句而不是 MySQL 插入 BLOB C++ 连接器 API。
Looks like the issues is with the MySQL C++ connector.
LAST_INSERT_ID()
returns 0 whenthe ID field is null, explicit
insert.
LAST_INSERT_ID()
returns 0 whenthe ID field is not specified,
implicit insert.
I tried insert the
BLOB
(JPEG image) from the command line (monitor), and it works:For now, the workaround is to use SQL statements to insert the BLOB with a prepared statement rather than the MySQL C++ Connector API.
您不能指望在 mysql 命令行客户端上发出 SELECT LAST_INSERT_ID() 来为您提供在 C++ 代码中生成的最后插入的值。
LAST_INSERT_ID() 为您提供给定连接上最后一个自动生成的 ID。显然,您的 C++ 程序和 mysql 客户端使用 2 个不同的连接到 mysql 服务器。
(并且 LAST_INSERT_ID() 也应该在 INSERT 之后使用,INSERT 和 SELECTLAST_INSERT_ID() 之间不要有其他 SQL 语句)
You cannot expect issuing a
SELECT LAST_INSERT_ID()
on the mysql command line client to give you the last inserted value that was generated in your C++ code.LAST_INSERT_ID() gives you the last autogenerated ID on the given connection. Obviously your C++ program and the mysql client uses 2 different connections to the mysql server.
(and LAST_INSERT_ID() should be used after an INSERT too, don't have other SQL statements inbetwwen the INSERT and SELECTLAST_INSERT_ID())