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