dave boland wrote:
> Being a little paranoid, I like to insure that the db file exists

SQLite automatically creates an empty DB if you try to open
a nonexistent file, so you do not actually need to do anything.

> and what state it is in (unconfigured, so needs to be made
> ready; or ready to accept data (or be read)).  How do I do that?

Store a DB version number somewhere.  (You can do it like Android and use
PRAGMA user_version, or use an entry in some table.)  If the version
number is not high enough, you have to create or update the database.
Do everything in a transaction to prevent a partially-created/updated
database:

conn.isolation_level = None   # Python sucks
conn.execute('begin')
with conn:                    # automatically commits or rolls back the 
transaction
    version = conn.execute('PRAGMA user_version').fetchone()[0]

    if version < 1:
        conn.execute('CREATE TABLE foo(bar)')
        # ...
        conn.execute('PRAGMA user_version = 1')

    # optional: updates
    if version < 2:
        conn.execute('ALTER TABLE foo ADD COLUMN baz')
        # ...
        conn.execute('PRAGMA user_version = 2')


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
sqlite-users@mailinglists.sqlite.org
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to