added a dict_factory as a new row_factory for the database
This commit is contained in:
parent
7c15cf7353
commit
33758abd18
|
@ -1,3 +1,4 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
import os, sys
|
import os, sys
|
||||||
import sqlite3
|
import sqlite3
|
||||||
from pkg_resources import resource_string
|
from pkg_resources import resource_string
|
||||||
|
@ -10,6 +11,13 @@ def get_schema(version):
|
||||||
"db-schemas/v%d.sql" % version)
|
"db-schemas/v%d.sql" % version)
|
||||||
return schema_bytes.decode("utf-8")
|
return schema_bytes.decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
|
def dict_factory(cursor, row):
|
||||||
|
d = {}
|
||||||
|
for idx, col in enumerate(cursor.description):
|
||||||
|
d[col[0]] = row[idx]
|
||||||
|
return d
|
||||||
|
|
||||||
def get_db(dbfile, stderr=sys.stderr):
|
def get_db(dbfile, stderr=sys.stderr):
|
||||||
"""Open or create the given db file. The parent directory must exist.
|
"""Open or create the given db file. The parent directory must exist.
|
||||||
Returns the db connection object, or raises DBError.
|
Returns the db connection object, or raises DBError.
|
||||||
|
@ -20,7 +28,7 @@ def get_db(dbfile, stderr=sys.stderr):
|
||||||
db = sqlite3.connect(dbfile)
|
db = sqlite3.connect(dbfile)
|
||||||
except (EnvironmentError, sqlite3.OperationalError) as e:
|
except (EnvironmentError, sqlite3.OperationalError) as e:
|
||||||
raise DBError("Unable to create/open db file %s: %s" % (dbfile, e))
|
raise DBError("Unable to create/open db file %s: %s" % (dbfile, e))
|
||||||
db.row_factory = sqlite3.Row
|
db.row_factory = dict_factory
|
||||||
|
|
||||||
VERSION = 2
|
VERSION = 2
|
||||||
if must_create:
|
if must_create:
|
||||||
|
@ -30,7 +38,7 @@ def get_db(dbfile, stderr=sys.stderr):
|
||||||
db.commit()
|
db.commit()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
version = db.execute("SELECT version FROM version").fetchone()[0]
|
version = db.execute("SELECT version FROM version").fetchone()["version"]
|
||||||
except sqlite3.DatabaseError as e:
|
except sqlite3.DatabaseError as e:
|
||||||
# this indicates that the file is not a compatible database format.
|
# this indicates that the file is not a compatible database format.
|
||||||
# Perhaps it was created with an old version, or it might be junk.
|
# Perhaps it was created with an old version, or it might be junk.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user