Try-except for flow control in reading Sqlite

Burak Arslan burak.arslan at arskom.com.tr
Mon Oct 28 04:17:26 EDT 2013


On 10/28/13 05:43, Victor Hooi wrote:
> Hi,
>
> I'd like to double-check something regarding using try-except for controlling flow.
>
> I have a script that needs to lookup things in a SQLite database.
>
> If the SQLite database file doesn't exist, I'd like to create an empty database, and then setup the schema.
>
> Is it acceptable to use try-except in order to achieve this? E.g.:
>
>     try:
>         # Try to open up the SQLite file, and lookup the required entries
>     except OSError:
>         # Open an empty SQLite file, and create the schema
>

this doesn't protect against a partially-created schema. do you have
something like a "version" table in your database as the last created
table? you can check for its existence in the except block and if it's
not there, you should remove the file and re-create it.

to get a list of tables:

    select * from sqlite_master where type='table';

best,
burak




More information about the Python-list mailing list