When connecting to an SQLite database from Python (using Python 2.6), what strategies are there to ensure that a program has opened a valid database file (where valid here means “matches what the program is expecting”)?
I’d like to make sure that after some opening checks I can be (reasonably) sure that the program has opened a database file that will work as expected – ideally CREATEing everything if the file was new/empty and stopping/warning if the file is a database for something else, or otherwise broken.
I’m guessing the trick is to compare the opened file’s schema to an expected schema in the program?
If so, how would you go about doing?
Otherwise, what else should be done?
In solutions where I want something similar, I tend to keep a
.sqlfile located at the same directory as my code, containing the build instructions for the database, using constructs similar to the following:just taking care that all SQL statements end with a semicolon as the last non-whitespace character of a line, since I have code like the following method that ensures the schema running every time my application starts:
Note the use of
CREATE TABLE IF NOT EXISTSandCREATE INDEX IF NOT EXISTS.