[Python] how to tell if cursor is sqlite.Cursor or psycopg2.Cursor

Chris Gonnerman chris.gonnerman at newcenturycomputers.net
Mon Jan 24 23:52:13 EST 2011


You're looking at it wrong.  It doesn't matter what type of cursor it 
is, only if you can get the correct number.  So use a try...except:

try:
     cursor.execute("""
         select last_insert_rowid()
     """)
except:
     cursor.execute("""
         select currval('my_sequence')
     """)

That's just a quick-and-dirty example; you might need to pretty it up, 
or actually declare the type of exception you're expecting (always a 
good idea, but I didn't feel like looking up the right sqlite exception).

Good luck!



More information about the Python-list mailing list