Merge pull request #212 from 1398491991/master

fix SqliteDB : Cannot operate on a closed database.
This commit is contained in:
Urinx
2017-07-10 08:45:37 +08:00
committed by GitHub
+17 -1
View File
@@ -21,7 +21,7 @@ class SqliteDB(object):
def __init__(self, db_file):
self.db_file = db_file
self.conn = sqlite3.connect(db_file, check_same_thread=False)
# self.conn = sqlite3.connect(db_file, check_same_thread=False)
# use 8-bit strings instead of unicode string
self.conn.text_factory = str
# not return a tuple but a dict with column name as key
@@ -29,6 +29,22 @@ class SqliteDB(object):
# for thread-save
self.lock = threading.Lock()
def set_conn(self):
self._conn = sqlite3.connect(self.db_file,check_same_thread=False)
@property
def conn(self):
try:
self._conn.execute('select 1;')
# check out conn
except (sqlite3.ProgrammingError,AttributeError):
# Cannot operate on a closed database
self.set_conn()
finally:
return self._conn
def create_table(self, table, cols):
"""
@brief Creates a table in database