I am trying to create a function in Python 2.7.3 to open a SQLite database.
This is my code at the moment:
import sqlite3 as lite
import sys
db = r'someDb.sqlite'
def opendb(db):
try:
conn = lite.connect(db)
except sqlite3.Error:
print "Error open db.\n"
return False
cur = conn.cursor()
return [conn, cur]
I have tried the code above and I have observed that the sqlite3
library opens the database declared if exists, or creates a new database if this one doesn't exist.
Is there a way to check if the database exists with sqlite3
methods or I have to use file operation like os.path.isfile(path)
?
Yes, there is a way to do what you want with Python 3.4+.
Use the
sqlite3.connect()
function to connect, but pass it a URI instead of a file path, and addmode=rw
to its query string.Here is a complete working code example:
This will open an existing database from a file named
aaa.db
in the current folder, but will raise an error in case that file can not be opened or does not exist:Python sqlite.connect() docs state that:
Here's an excerpt of all the relevant URI option information collected from http://www.sqlite.org/c3ref/open.html:
For convenience, here's also a Python 3.4+ function for converting a regular path to an URI usable by sqlite.connect():
This is a fork (using Python 3) based on Tom Horen's answer, which presents a solution more complete and reliable that the elected answer.
The elected answer, does not evaluate any content, header, etc., in order to determine whether the file actually contains any data related to a SQLite3 database or not.
I tried to present something more pragmatic here:
I am using a function like the following at the beginning of my script so that I can try and figure out why a sqlite3 db script might not be working. Like the comments say, it uses 3 phases, checks if a path exist, checks if the path is a file, checks if that file's header is a sqlite3 header.
os.path.isfile()
is just telling you if a file exists, not if it exists AND is a SQLite3 database! Knowing http://www.sqlite.org/fileformat.html, you could do this :and subsequently use it like :
In Python 2, you'll have to explicitly test for the existence using
os.path.isfile
:There is no way to force the
sqlite3.connect
function to not create the file for you.For those that are using Python 3.4 or newer, you can use the newer URI path feature to set a different mode when opening a database. The
sqlite3.connect()
function by default will open databases inrwc
, that is Read, Write & Create mode, so connecting to a non-existing database will cause it to be created.Using a URI, you can specify a different mode instead; if you set it to
rw
, so Read & Write mode, an exception is raised when trying to connect to a non-existing database. You can set different modes when you set theuri=True
flag when connecting and pass in afile:
URI, and add amode=rw
query parameter to the path:See the SQLite URI Recognized Query Parameters documentation for more details on what parameters are accepted.