SQLite database files

SQLite will try to recreate the database file every time you run the program. If the DB file already exists, you will get an OperationalError exception. The easiest way to deal with this is to simply catch the exception and ignore it, as demonstrated here.

Dealing with existing databases:

try:
cursor.execute("CREATE TABLE Foo (id INTEGER PRIMARY KEY, name TEXT)")
except sqlite3.OperationalError:
pass

This will allow you to run your database program multiple times (such as during creation or testing) without having to delete the DB file after every run.

You can also use a similar try/except block when testing to see if the DB file already exists; if the file doesn't exist, then you can call the DB creation module. This allows you to put the DB creation code in a separate module from the main program, calling it only when needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.120.57