From 33758abd18d9a2fb728f9c116396c3233a8f423d Mon Sep 17 00:00:00 2001 From: laharah Date: Fri, 3 Jun 2016 16:47:36 -0700 Subject: [PATCH] added a dict_factory as a new row_factory for the database --- src/wormhole/server/database.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/wormhole/server/database.py b/src/wormhole/server/database.py index 163c8cd..253b536 100644 --- a/src/wormhole/server/database.py +++ b/src/wormhole/server/database.py @@ -1,3 +1,4 @@ +from __future__ import unicode_literals import os, sys import sqlite3 from pkg_resources import resource_string @@ -10,6 +11,13 @@ def get_schema(version): "db-schemas/v%d.sql" % version) 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): """Open or create the given db file. The parent directory must exist. Returns the db connection object, or raises DBError. @@ -20,7 +28,7 @@ def get_db(dbfile, stderr=sys.stderr): db = sqlite3.connect(dbfile) except (EnvironmentError, sqlite3.OperationalError) as 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 if must_create: @@ -30,7 +38,7 @@ def get_db(dbfile, stderr=sys.stderr): db.commit() try: - version = db.execute("SELECT version FROM version").fetchone()[0] + version = db.execute("SELECT version FROM version").fetchone()["version"] except sqlite3.DatabaseError as e: # this indicates that the file is not a compatible database format. # Perhaps it was created with an old version, or it might be junk.