exception handling with sqlite db errors

CM cmpython at gmail.com
Sun Aug 1 22:34:58 CEST 2010

I am using SQLite with Python 2.5 for an app and every now and then
get an error when trying to write to the database.  So far I haven't
been careful about noting these down, but now I would like to address
them.  The two errors I've noticed are:

1) "database is locked" errors (probably due to write conflicts when
using SQLite Database Browser to check and change the data while my
app was trying to write to the db; this might be a problem for users
if they too want to edit their data that way while running the app,
though that scenario is pretty unlikely).

2) Today for the first time,"sqlite3.OperationalError: attempt to
write a readonly database"--though I never set this database to be
read-only. When I tried it a second time, it worked and didn't give
this error.

What I would like is a way to innoculate my app from these errors such
that if they occur they don't cause any problems with the app.  I'm
not even sure if they do cause problems, since I can't figure out a
way to reproduce the errors and see whether it causes the app to hang
or not update the GUI properly, etc.

Maybe it's not much of an issue, but I think it would be a shame if
occasional hangs/crashes could be caused by these (rare?) database
conflicts if there is a good approach for avoiding them.  I guess I
could put every last write to the db in a try/except block but I
thought there should be a more general solution, since that will
require many such exceptions and seems inelegant.

Any suggestions for addressing this are appreciated.


More information about the Python-list mailing list