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

Discussion in 'Python' started by dmaziuk, Jan 24, 2011.

  1. dmaziuk

    dmaziuk Guest

    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
    dmaziuk, Jan 24, 2011
    #1
    1. Advertising

  2. dmaziuk

    Jon Clements Guest

    On Jan 24, 7:44 pm, 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?
    >
    > 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.
    Jon Clements, Jan 24, 2011
    #2
    1. Advertising

  3. dmaziuk

    MRAB Guest

    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>'
    MRAB, Jan 24, 2011
    #3
  4. Re: [Python] how to tell if cursor is sqlite.Cursor or psycopg2.Cursor

    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!
    Chris Gonnerman, Jan 25, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ASh
    Replies:
    10
    Views:
    2,380
    Anton Shishkov
    Mar 31, 2010
  2. mrdrew
    Replies:
    5
    Views:
    2,689
    Dennis Lee Bieber
    Apr 5, 2010
  3. Carl Youngblood
    Replies:
    1
    Views:
    213
    Carl Youngblood
    Apr 9, 2005
  4. Replies:
    4
    Views:
    336
  5. Replies:
    6
    Views:
    236
    Walter Hurry
    Jan 6, 2013
Loading...

Share This Page