Menu

insert_id not working?

Help
2007-03-29
2012-09-19
  • David Kramer

    David Kramer - 2007-03-29

    db.insert_id() is always returning 0 for me, while "SELECT LAST_INSERT_ID()" is returning the correct value. I would rather not do a whole other query. Has anyone else seen that?

    Thanks.

    Red Hat RHEL4
    mysql-4.1.7
    python-2.3.4-14
    MySQL-python-1.2.0
    (No, I can't upgrade any of these. My client needs this specific set of packages.)

     
    • Andy Dustman

      Andy Dustman - 2007-03-29

      Don't use db.insert_id(); that's used internally. Instead:

      http://www.python.org/dev/peps/pep-0249/

      Cursor Attribute .lastrowid
      
          This read-only attribute provides the rowid of the last
          modified row (most databases return a rowid only when a single
          INSERT operation is performed). If the operation does not set
          a rowid or if the database does not support rowids, this
          attribute should be set to None.
      
          The semantics of .lastrowid are undefined in case the last
          executed statement modified more than one row, e.g. when
          using INSERT with .executemany().
      
       
    • David Kramer

      David Kramer - 2007-03-29

      Thanks! I'm fixing someone else's code, as well as porting it from Windows to Linux, and they were using insert_id(). Sorry I didn't see this one in the docs. Serves me right for trusting Google first.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.