[Tutor] SQLite error messages

Benno Lang transmogribenno at gmail.com
Wed Mar 10 04:56:35 CET 2010


On 10 March 2010 11:37, Alan Harris-Reid <aharrisreid at googlemail.com> wrote:
> Hi there,
>
> I am using the sqlite3 module with Python 3.1, and have some code which goes
> something like as follows...
>
> import sqlite3
> con = sqlite3.connect('MyDatabase.db')
>
> try:
>   execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
> ("MyValue")')
>   con.commit()
> except:
>   con.rollback()
>   If con.execute() fails, nothing is returned, and although the code
> correctly executes the rollback next, I have no idea why, and therefore
> cannot create a suitable error-handler with meaningful messages.
> I notice from the SQLite website that there are error codes, but it looks
> like the sqlite3 module is not reporting them.

Do you mean numerical error codes? Which page on the SQLite website
are you referring to? Certainly the exception contains usable data.
Try something like this:

try:
  execresult = con.execute('INSERT INTO MyTable (field_name) VALUES
("MyValue")')
  con.commit()
except Exception as error:
  print("Didn't work:", error)
  con.rollback()

(I didn't create a table, so I get "Didn't work: no such table: MyTable")

HTH,
benno


More information about the Tutor mailing list