I'm using the mysql.connector module for Python 2.7. I have broken my code down to the simplest possible script, but I am still having this problem. The problem is that when I try to get the last row id (which would be LAST_INSERT_ID
in MySQL), I get a return of 0, regardless of how many rows have been inserted. Does anyone have the solution for this problem?
My code is as follows:
import mysql.connector
default_config = {
'user': 'root',
'password': 'password',
'host': '127.0.0.1',
'database': 'test',
'raise_on_warnings': True,
'autocommit': True
}
connection = mysql.connector.connect(**default_config)
cursor = connection.cursor()
args = ('name', 'desc')
cursor.callproc('sp_tools_insert', args)
lastid = cursor.lastrowid
print lastid # This returns 0 every time, regardless of number of inserts
My stored procedure looks like this:
CREATE PROCEDURE `sp_tools_insert`
(
IN p_name VARCHAR(45),
IN p_description VARCHAR(255)
)
BEGIN
INSERT INTO TOOLS
(
tool_name,
description
)
VALUES
(
p_name,
p_description
);
END
This is how my TOOLS
table is defined:
DROP TABLE IF EXISTS `test`.`TOOLS` ;
CREATE TABLE IF NOT EXISTS `test`.`TOOLS` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`tool_name` VARCHAR(45) NOT NULL,
`description` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
I have verified that the stored procedure is working correctly and the .callproc()
call works as expected. The only thing not working is the .lastrowid
call. Any suggestions?