[DB-SIG] Mysql insert_id() questions

Andy Dustman adustman@comstar.net
Mon, 6 Dec 1999 15:49:01 -0500 (EST)


On Thu, 2 Dec 1999, Andrew Diller wrote:

> 
> Hello:
> 
> I'm trying to grab the auto_increment value of my most recent insert. The
> MySQL manuals and the MySQLdb both mention the mysql_insert.id() which
> does exactly this.
> 
> I'm getting an error though, in my cgi code:
> 
> ------------------
> 
> db = MySQLdb.Connection(db='example', user='test', passwd='dontlook');
> cursor = db.cursor()
> 
> .... sometime later:
> 
> cursor.execute("INSERT into database some stuff")
> 
> recordKey = db.insert_id()
> 
> 
> 
> 
> I get an attribute error on the assignment:
> 
> 
> File "/usr/local/apache/htdocs/usginfo/incident.py", line 142, in
> ProcessForm
>     recordKey = db.insert_id()
> AttributeError: insert_id
> 
> 
> Does anyone have a sucessful code snippet to retreive this value from
> MySQL?

You actually need:

recordKey = db.db.insert_id()

insert_id() is a method of a _mysql connection, which is stored in the
MySQLdb.Connection() as db.

Keep in mind that all the methods listed in the table in big table (MySQL
C API function mapping) in the MySQLdb docs are methods of various _mysql
objects, which is not a DB API interface (supplied by MySQLdb). The
documentation on this methods is intentionally vague, but should be fairly
obvious if you have the standard MySQL docs.

-- 
andy dustman       |     programmer/analyst     |      comstar.net, inc.
telephone: 770.485.6025 / 706.549.7689 | icq: 32922760 | pgp: 0xc72f3f1d
"Therefore, sweet knights, if you may doubt your strength or courage, 
come no further, for death awaits you all, with nasty, big, pointy teeth!"