Hello all,
I needed to do some Python database work. I set up for it on my Mandrake
Linux 10 box by
makeing sure MySQL 4.1 was working correctly (yes), installing MySQLdb
1.1.1 from
http://sourceforge.net/projects/mysql-python and making sure it worked
correctly (yes),
then installing SQLObject 0.5.2. After going thru the learning curve on
SQLObject,
I hit a brick wall, where I'd get a AttributeError exception, telling me
that
a Cursor object does not have an 'insert_id' method. I then got the
latest version
of SQLObject from the svn 'trunk' repository. This did not fix the
problem.
Examining the SQLObject source, I found that the _queryInsertID method of
mysql/mysqlconnection.py
does call insert_id() as a method of an instance of the cursor class.
Examining the MySQLdb source,
I found that the Cursor class does not have such a method, that instead it
is a method of the Connection
class. When a cursor's execute() method is called, the code calls the
connections insert_id()
method, and then stores the value in the cursor's 'lastrowid' attribute.
So the solution for SQLObject appears to be to use the 'lastrowid'
attribute of the cursor class,
rather than trying to call the cursor class's non-existant insert_id()
method.
When I made the following patch, the problem disappeared. I'd appreciate
it if someone could
check me on this. It seems strange that this problem could exist. Surely
someone else
must be using SQLObject with MySQL.
Index: sqlobject/mysql/mysqlconnection.py
===================================================================
--- sqlobject/mysql/mysqlconnection.py (revision 163)
+++ sqlobject/mysql/mysqlconnection.py (working copy)
@@ -50,7 +50,7 @@
self.printDebug(conn, q, 'QueryIns')
self._executeRetry(conn, c, q)
if id is None:
- id = c.insert_id()
+ id = c.lastrowid
if self.debugOutput:
self.printDebug(conn, id, 'QueryIns', 'result')
return id
--
Give a man a fish, and he'll eat for a day.
TEACH a man to fish, and he'll decide it's too much trouble, and take your
fish.
|