使用准备好的语句后 SELECT LAST_INSERT_ID() 返回 0

发布于 2024-10-19 10:43:42 字数 2085 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

べ映画 2024-10-26 10:43:43

LAST_INSERT_ID 仅适用于在 auto_increment 字段上创建的自动生成的主键。在您的情况下,您似乎显式提供了 id,因此未设置最后插入 id。

这是显式的:

mysql> insert into test (id, name) VALUES (5, 'test 2');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

这是隐式的:

mysql> insert into test (name) values ('test');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

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:

mysql> insert into test (id, name) VALUES (5, 'test 2');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                0 |
+------------------+
1 row in set (0.00 sec)

This is implicit:

mysql> insert into test (name) values ('test');
Query OK, 1 row affected (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)
想念有你 2024-10-26 10:43:43

看起来问题出在 MySQL C++ 连接器上。

  1. LAST_INSERT_ID() 在以下情况下返回 0:
    ID 字段为空,显式
    插入。
  2. LAST_INSERT_ID() 在以下情况下返回 0:
    未指定 ID 字段,
    隐式插入。

我尝试从命令行(监视器)插入 BLOB(JPEG 图像),并且它有效:

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.03 sec)
mysql> PREPARE blob_stmt
    ->     FROM 'INSERT INTO picture_image_data (ID_Image_Data, Image_Data) VALUES (?, LOAD_FILE(?))';
Query OK, 0 rows affected (0.02 sec)
Statement prepared

mysql> SET @id = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @c = 'KY_hot_brown_picture.jpg';
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE blob_stmt USING @id, @c;
Query OK, 1 row affected (0.15 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                2 |
+------------------+

1 row in set (0.00 sec)

目前,解决方法是使用 SQL 语句通过准备好的语句而不是 MySQL 插入 BLOB C++ 连接器 API。

Looks like the issues is with the MySQL C++ connector.

  1. LAST_INSERT_ID() returns 0 when
    the ID field is null, explicit
    insert.
  2. LAST_INSERT_ID() returns 0 when
    the ID field is not specified,
    implicit insert.

I tried insert the BLOB (JPEG image) from the command line (monitor), and it works:

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.03 sec)
mysql> PREPARE blob_stmt
    ->     FROM 'INSERT INTO picture_image_data (ID_Image_Data, Image_Data) VALUES (?, LOAD_FILE(?))';
Query OK, 0 rows affected (0.02 sec)
Statement prepared

mysql> SET @id = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SET @c = 'KY_hot_brown_picture.jpg';
Query OK, 0 rows affected (0.00 sec)

mysql> EXECUTE blob_stmt USING @id, @c;
Query OK, 1 row affected (0.15 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                2 |
+------------------+

1 row in set (0.00 sec)

For now, the workaround is to use SQL statements to insert the BLOB with a prepared statement rather than the MySQL C++ Connector API.

以为你会在 2024-10-26 10:43:43

您不能指望在 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())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文