psycopg2 or pygresql?

E

exhuma.twn

Plain and simple. What would you use?

So far I have written everything with psycopg2. One thing that annoys
me is that I cannot easily access the column names from a query. I
know that this is not part of the DBAPI2 so I cannot expect the model
to suport it.

I quite like the "mogrify" method of psycopg2 very much. It's very
useful for debugging.

And before someone says: "Use the DictCursor-factory to access column
names". Yes I can do that. But a dict does not guarantee the same
order of keys as the columns were specified in the query.

The reason: I wrote a very simple Qt-Widget (inherited from
QTableWidget) that takes a SQL query and displays the results. And I
would like to have the columns ordered the same way as I specified in
the query *with* a header-label. For now I hava a method on the widget
which takes a query, *and* a list of labels.

I can live with that though. Although it itches me.

Would pygresql solve my dilemma? Or would you rather say: "Don't use
pygresql!" ;)
 
J

jay graves

And before someone says: "Use the DictCursor-factory to access column
names". Yes I can do that. But a dict does not guarantee the same
order of keys as the columns were specified in the query.

But you can iterate over the cursor.description sequence and then look
up the column value (by name or index). I haven't specifically tried
this with your setup but I have used it with various DBAPI2 compliant
modules. (SQLite, pyodbc, win32 odbc, etc) There are lots of goodies
in the cursor.description but each implementation varies on what it
provides. Check out the PEP for more info.

http://www.python.org/dev/peps/pep-0249/

HTH.
Jay
 
S

Steve Holden

exhuma.twn said:
Plain and simple. What would you use?

So far I have written everything with psycopg2. One thing that annoys
me is that I cannot easily access the column names from a query. I
know that this is not part of the DBAPI2 so I cannot expect the model
to suport it.
Yes it is.

Execute a SELECT * FROM TABLE WHERE 1=0 and then examine
cursor.description. This allows you to introspect on database structure.
I quite like the "mogrify" method of psycopg2 very much. It's very
useful for debugging.

And before someone says: "Use the DictCursor-factory to access column
names". Yes I can do that. But a dict does not guarantee the same
order of keys as the columns were specified in the query.
See below.
The reason: I wrote a very simple Qt-Widget (inherited from
QTableWidget) that takes a SQL query and displays the results. And I
would like to have the columns ordered the same way as I specified in
the query *with* a header-label. For now I hava a method on the widget
which takes a query, *and* a list of labels.

I can live with that though. Although it itches me.

Would pygresql solve my dilemma? Or would you rather say: "Don't use
pygresql!" ;)
Here's a psycopg2-based session:
(('psnid', 23, None, 4, None, None, None), ('psnfirstname', 1043, None,
50, None, None, None), ('psnlastname', 1043, None, 50, None, None,
None), ('psndear', 1043, None, 50, None, None, None), ('psntitle', 1043,
None, 50, None, None, None), ('psnworkphone', 1043, None, 30, None,
None, None), ('psnworkextension', 1043, None, 20, None, None, None),
('psnhomephone', 1043, None, 30, None, None, None), ('psnmobilephone',
1043, None, 30, None, None, None), ('psnfaxnumber', 1043, None, 30,
None, None, None), ('psnemailname', 1043, None, 50, None, None, None),
('psnreferredby', 1043, None, 50, None, None, None), ('psnlocid', 23,
None, 4, None, None, None), ('psnnotes', 1043, None, -1, None, None, None))

I believe if you use specific column names in your query they will
appear in the order given in the query also.

I use this technique in

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81189

to print arbitrary query outputs.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 
E

exhuma.twn

Yes it is.

Execute a SELECT * FROM TABLE WHERE 1=0 and then examine
cursor.description. This allows you to introspect on database structure.



See below.




Here's a psycopg2-based session:

(('psnid', 23, None, 4, None, None, None), ('psnfirstname', 1043, None,
50, None, None, None), ('psnlastname', 1043, None, 50, None, None,
None), ('psndear', 1043, None, 50, None, None, None), ('psntitle', 1043,
None, 50, None, None, None), ('psnworkphone', 1043, None, 30, None,
None, None), ('psnworkextension', 1043, None, 20, None, None, None),
('psnhomephone', 1043, None, 30, None, None, None), ('psnmobilephone',
1043, None, 30, None, None, None), ('psnfaxnumber', 1043, None, 30,
None, None, None), ('psnemailname', 1043, None, 50, None, None, None),
('psnreferredby', 1043, None, 50, None, None, None), ('psnlocid', 23,
None, 4, None, None, None), ('psnnotes', 1043, None, -1, None, None, None))

I believe if you use specific column names in your query they will
appear in the order given in the query also.

I use this technique in

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/81189

to print arbitrary query outputs.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline


Apparently I am deaf dumb and blind.... :( Sorry. I grepped several
times through the PEP for various other reasons, and this little bit
escaped me.

Thanks for being nice guys and answering anyhow. Much appreciated.
 
S

Steve Holden

exhuma.twn wrote:
[...]
Apparently I am deaf dumb and blind.... :( Sorry. I grepped several
times through the PEP for various other reasons, and this little bit
escaped me.
You forgot "stupid" ;-)
Thanks for being nice guys and answering anyhow. Much appreciated.

A pleasure.

been-known-to-act-stupid-myself-ly y'rs - steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden

Sorry, the dog ate my .sigline
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top