How do you safely and efficiently get the row id after an insert with mysql using MySQLdb in python?

后端 未结 4 561
独厮守ぢ
独厮守ぢ 2020-12-14 03:22

I have a simple table in mysql with the following fields:

  • id -- Primary key, int, autoincrement
  • name -- varchar(50)
  • description -- varchar(25
4条回答
  •  执笔经年
    2020-12-14 04:15

    I don't know if there's a MySQLdb specific API for this, but in general you can obtain the last inserted id by SELECTing LAST_INSERT_ID()

    It is on a per-connection basis, so you don't risk race conditions if some other client performs an insert as well.

提交回复
热议问题