python pg module error messages

1.2k views Asked by At

pg module in python for interacting with postgres is not giving any error message for DML queries.

Is there any alternative to pg module which gives meaningful error messages.

>>>import pg 
>>>
>>>
>>>conn = pg.connect(dbname="db", user="postgres", host="localhost")
>>>print conn.query("delete from item where item_id=0")
>>>None
>>>print conn.query("delete from item where item_id=0")
>>>None                     #This should have been an error message
2

There are 2 answers

0
Thales MG On

Do you know the psycopg2 module? It seems a very nice module to interact with PostgreSQL via Python. There is a tutorial available, besides the modules' documentation. In the examples given in the docs, commands that fail indeed print out error messages.

I personally don't have experience in this module. But it looks very nice!

3
Chris Travers On

Why are you expecting an error message? I delete does not raise an error in the server if no records were found. So why do you expect a generally applicable database driver to raise an error?

I can't think of any database driver that would issue an error in that case because there may be perfectly legitimate reasons for it. In database terms, for example, an error usually means you are going to roll back a transaction.

If you are going to wrap in your own API, my recommendation is that you either decide how you want your application to address this or at most you raise warnings instead.