SELECT LAST_INSERT_ID() returns 0 after using prepared statement

强颜欢笑 提交于 2019-12-07 11:57:03

问题


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).

|improve this question

回答1:


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)



回答2:


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.




回答3:


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())



来源:https://stackoverflow.com/questions/5124546/select-last-insert-id-returns-0-after-using-prepared-statement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!