[Tutor] SQLite error messages

Alan Harris-Reid aharrisreid at googlemail.com
Wed Mar 10 20:32:16 CET 2010


Benno Lang wrote:
> 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
Hi Benno,  your example is great - just what I needed!

Regarding SQLite error codes, the list I was referring to is at 
www.sqlite.org/c3ref/c_abort.html 
<http://www.sqlite.org/c3ref/c_abort.html>, but it doesn't look complete 
because I have already come-across some IntegrityError messages which 
aren't on the list.

Regards,
Alan




-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100310/4e498e79/attachment.html>


More information about the Tutor mailing list