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

后端 未结 4 568
独厮守ぢ
独厮守ぢ 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 think it might be

    newID = db.insert_id()
    

    Edit by Original Poster

    Turns out, in the version of MySQLdb that I am using (1.2.2) You would do the following:

    conn = MySQLdb(host...)
    
    c = conn.cursor()
    c.execute("INSERT INTO...")
    newID = c.lastrowid
    

    I am leaving this as the correct answer, since it got me pointed in the right direction.

提交回复
热议问题