Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the IDENTITY / AUTONUMBER value for the row I inserted in pymysql

Tags:

python

mysql

Is possible to get ID for the row I inserted using pymysql?

curr = db.cursor()
curr.execute("INSERT INTO `accounts` (`name`, `password`) VALUES ('%s', '%s')", accName, passwd)
curr.execute("INSERT INTO `person` (`name`, `accoiunt_id`) VALUES ('%s', '%d')", pName, HERE_I_NEED_ACCID)

There is autoincrement primary key "id" in accounts table.

like image 612
Kiro Avatar asked Dec 07 '22 20:12

Kiro


1 Answers

You can use lastrowid property of your cursor object.

Or you can execute SELECT LAST_INSERT_ID(), and fetch the result as scalar with fetchone()

like image 151
Imran Avatar answered Dec 25 '22 07:12

Imran