how to tell if cursor is sqlite.Cursor or psycopg2.Cursor

MRAB python at mrabarnett.plus.com
Mon Jan 24 15:46:09 EST 2011


On 24/01/2011 19:44, dmaziuk wrote:
> 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?
>
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>'



More information about the Python-list mailing list