Discussion:
how to tell if cursor is sqlite.Cursor or psycopg2.Cursor
(too old to reply)
dmaziuk
2011-01-24 19:44:55 UTC
Permalink
Hi everyone,

I've wrapper class around some sql statements and I'm trying to add a
method that does:
if my_cursor is a sqlite cursor, then run "select
last_insert_rowid()"
else if it's a psycopg2 cursor, then run "select
currval( 'my_sequence' )"
etc.
The best I can come up with is import both psycopg2 and sqlite and
then do
if isinstance( self._curs, sqlite.Cursor ) : ...
elif isinstance( self._curs, psycopg2._psycopg.cursor ) : ...
and I can't help thinking there has to be another way to find out what
kind of thing self._curs is. Is there a better way?

TIA
Dima
Jon Clements
2011-01-24 20:41:46 UTC
Permalink
Post by dmaziuk
Hi everyone,
I've wrapper class around some sql statements and I'm trying to add a
  if my_cursor is a sqlite cursor, then run "select
last_insert_rowid()"
  else if it's a psycopg2 cursor, then run "select
currval( 'my_sequence' )"
  etc.
The best I can come up with is import both psycopg2 and sqlite and
then do
 if isinstance( self._curs, sqlite.Cursor ) : ...
 elif isinstance( self._curs, psycopg2._psycopg.cursor ) : ...
and I can't help thinking there has to be another way to find out what
kind of thing self._curs is. Is there a better way?
TIA
Dima
I'm not 100% sure but maybe look at SQLAlchemy (or other Python ORMs)
as a wrapper. That *might* abstract the "last ID" across different
DB's. And still enable direct SQL querying.

Jon.
MRAB
2011-01-24 20:46:09 UTC
Permalink
Post by dmaziuk
Hi everyone,
I've wrapper class around some sql statements and I'm trying to add a
if my_cursor is a sqlite cursor, then run "select
last_insert_rowid()"
else if it's a psycopg2 cursor, then run "select
currval( 'my_sequence' )"
etc.
The best I can come up with is import both psycopg2 and sqlite and
then do
if isinstance( self._curs, sqlite.Cursor ) : ...
elif isinstance( self._curs, psycopg2._psycopg.cursor ) : ...
and I can't help thinking there has to be another way to find out what
kind of thing self._curs is. Is there a better way?
I quick hack might be to look at repr(my_cursor). For sqlite3 I get:

'<built-in method cursor of sqlite3.Connection object at 0x01676320>'
dmaziuk
2011-01-24 23:25:01 UTC
Permalink
For psycopg2: '<cursor object at 0x2b60b6066a48; closed: 0>'
(of course, this could also be due to RHEL5's ancient python).

Dima
Chris Gonnerman
2011-01-25 04:52:13 UTC
Permalink
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!
dmaziuk
2011-01-24 23:25:01 UTC
Permalink
For psycopg2: '<cursor object at 0x2b60b6066a48; closed: 0>'
(of course, this could also be due to RHEL5's ancient python).

Dima
dmaziuk
2011-01-25 17:22:05 UTC
Permalink
D'oh. You're right, of course.

Thank you
Dima

Loading...