我的代码错误:
ProgrammingError:在线程中创建的SQLite对象只能在同一线程中使用。该对象是在线程ID 4480339264中创建的,这是线程ID 123145516933120。
这是我的代码
文件app.py
from db import Database
app.db = Database()
def train():
# save to our sqlite database.db
created = int(time.time())
user_id = app.db.insert('INSERT INTO users(name, created) values(?,?)', [name, created])
文件db.py
import sqlite3
from os import path, getcwd
db = path.join(getcwd(), 'database.db')
class Database:
def __init__(self):
self.connection = sqlite3.connect(db)
def query(self, q, arg=()):
cursor = self.connection.cursor()
cursor.execute(q, arg)
results = cursor.fetchall()
cursor.close()
return results
def insert(self, q, arg=()):
cursor = self.connection.cursor()
cursor.execute(q, arg)
self.connection.commit()
result = cursor.lastrowid
cursor.close()
return result
方法insert cursor = self.connection.cursor()中的文件db.py中的错误