From 8cd140918e61c162734b0ed44eb2b7691f633c19 Mon Sep 17 00:00:00 2001 From: lshu <1398491991@qq.com> Date: Fri, 7 Jul 2017 10:06:09 +0800 Subject: [PATCH] fix SqliteDB : Cannot operate on a closed database. --- wxbot_project_py2.7/db/sqlite_db.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/wxbot_project_py2.7/db/sqlite_db.py b/wxbot_project_py2.7/db/sqlite_db.py index 5abc522..b953721 100644 --- a/wxbot_project_py2.7/db/sqlite_db.py +++ b/wxbot_project_py2.7/db/sqlite_db.py @@ -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