[Tutor] SQLite error messages

Alan Harris-Reid aharrisreid at googlemail.com
Wed Mar 10 21:02:22 CET 2010


Sander Sweers wrote:
> ----- Original message -----
>   
>> 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:
>>     
>
> Here you catch all exceptions. Normally you would catch a specific exception like ValueError.  
>   
>>         con.rollback()
>>
>>     
>
> Do you know finally? It is run after all the exceptions have been handled and this is where I would put the rollback.
>
> Greets,
> Sander

Hello Sander, thanks for the reply.

"Normally you would catch a specific exception like ValueError."
Agreed, but as I don't know what type the exception is, I would have to 
provide a suitable error message for all exception types (ValueError, 
IntegrityError, etc.).  At this stage catching Exception as errormessage 
is sufficient for my purposes.

"Do you know finally? It is run after all the exceptions have been 
handled and this is where I would put the rollback."
In this case there is no 'finally' section, because if the 'try' section 
doesn't work, then I want the rollback to occur for *all *exceptions. 

Maybe I have misunderstood you, but I always thought that the 'finally' 
section was run even if the 'try' section is successful, in which case I 
would not want a rollback.  (According to the Python documentation 
(section 8.6) "A /finally clause/ is always executed before leaving the 
try <http://docs.python.org/reference/compound_stmts.html#try> 
statement, whether an exception has occurred or not.").

Regards,
Alan



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


More information about the Tutor mailing list