0

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
Milen A. Radev
  • 60,241
  • 22
  • 105
  • 110
Vineet Menon
  • 728
  • 2
  • 9
  • 25
  • According to the [documentation](http://www.pygresql.org/pg.html#delete-delete-a-row-from-a-database-table) you can use `delete` instead of `query` which will return to you the number of rows that were deleted. In your case a return value of `0` (for no rows deleted) would indicate an "error". –  Sep 06 '13 at 12:39
  • Thanks for the input. the API which you are referring to belongs to wrapper class. I was thinking of something with the basic function. I'll have to refractors the code. – Vineet Menon Sep 06 '13 at 12:42
  • Try committing the transaction. – Burhan Khalid Nov 13 '13 at 05:48

2 Answers2

2

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.

Chris Travers
  • 25,424
  • 6
  • 65
  • 182
  • Okay. I thought since the instructions for deletion/insertion were not successful, I thought it should raise an exception or an error code. What are my options btw. – Vineet Menon Nov 16 '13 at 12:13
  • catch the number of rows inserted and deleted and decide what to do with that.... – Chris Travers Nov 16 '13 at 12:20
  • (What you decide to do may well depend on what you are doing, and sometimes you might want to raise an exception and sometimes you might not) – Chris Travers Nov 16 '13 at 12:20
0

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!

Thales MG
  • 761
  • 5
  • 15