Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return the last primary key after INSERT in pymysql (python3.5)?

CREATE TABLE `users` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `email` varchar(255) COLLATE utf8_bin NOT NULL,
    `password` varchar(255) COLLATE utf8_bin NOT NULL,
    PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;

for example, how to get the primary key id of the last record that I insert into the table by cursor.execute("insert into ...", ...)?

like image 357
xaero Avatar asked Mar 12 '23 03:03

xaero


1 Answers

after inserting,You can get it as: cursor.execute('select LAST_INSERT_ID()') or use cursor.lastrowid

like image 116
shivsn Avatar answered Mar 15 '23 10:03

shivsn