diff --git a/SimpaDbUtil.py b/SimpaDbUtil.py index fdaaaef..6cc316d 100644 --- a/SimpaDbUtil.py +++ b/SimpaDbUtil.py @@ -84,7 +84,12 @@ class SqlFile(ISqlDatabaseBackend): os.remove(sqlite_db_path) except: pass - self._con = sqlite3.connect(sqlite_db_path) + check_same_thread = False + # this is to prevent the following error when run from apache/django : SQLite objects created in a thread can only be used in that same thread.The object was created in thread id 139672342353664 and this is thread id 139672333960960 + # accordig to https://stackoverflow.com/questions/48218065/programmingerror-sqlite-objects-created-in-a-thread-can-only-be-used-in-that-sa this is ok, as long as there are no concurrent writes + # If set False, the returned connection may be shared across multiple threads. When using multiple threads with the same connection writing operations should be serialized by the user to avoid data corruption + # I hope it's safe here but I'm not 100% sure though. Anyway, if the database gets corrupt, it not a big deal since this memory resident database gets reconstructed from the sql file... + self._con = sqlite3.connect(sqlite_db_path, check_same_thread=check_same_thread) f = open(self._sql_file_path, 'r') sql = f.read() # watch out for built-in `str` #print(sql)