4

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.

Kiro
  • 920
  • 10
  • 24
  • possible duplicate of [How do you safely and efficiently get the row id after an insert with mysql using MySQLdb in python?](http://stackoverflow.com/questions/706755/how-do-you-safely-and-efficiently-get-the-row-id-after-an-insert-with-mysql-usin) – jhonkola May 07 '14 at 12:03

1 Answers1

15

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

Imran
  • 87,203
  • 23
  • 98
  • 131