Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > how to tell if cursor is sqlite.Cursor or psycopg2.Cursor

Reply
Thread Tools

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

 
 
dmaziuk
Guest
Posts: n/a
 
      01-24-2011
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
 
Reply With Quote
 
 
 
 
Jon Clements
Guest
Posts: n/a
 
      01-24-2011
On Jan 24, 7:44*pm, dmaziuk <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
 
MRAB
Guest
Posts: n/a
 
      01-24-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>'
 
Reply With Quote
 
Chris Gonnerman
Guest
Posts: n/a
 
      01-25-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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Some people tell that at present, most web hosting servers supportall kinds of programming language, some people tell me that many web hostingserver don't support Java, What is the truth? Erwin Moller Java 3 05-07-2008 05:09 PM
CAN any one tell it whats the code tell it yogesh C++ 1 03-14-2007 01:12 PM
Declare Cursor error while implementing SCROLL CURSOR invy C Programming 4 12-28-2006 02:35 PM
Changing DEFAULT cursor to WAIT cursor in ASP =?Utf-8?B?VG9tYXMgS2VwaWM=?= ASP .Net 1 04-05-2005 07:42 PM



Advertisments