好的,既然我在 MDI 应用程序中找出了整个持久性 QSqlDatabase,我尝试在 QThread 中访问它:
class LoadWorker(QThread):
totalSignal = pyqtSignal(int)
countSignal = pyqtSignal(int)
def __init__(self, parent=None):
super(LoadWorker, self).__init__(parent)
self.threadActive = True
self.commitsize = 200
self.filename = ""
def run(self):
query = QSqlQuery()
query.exec("DELETE FROM mytable")
#rest of code here
运行时,我得到一个错误:
QSqlDatabasePrivate::database: requested database does not belong to the calling thread.
QSqlQuery::exec: database not open
一些研究表明我应该为线程启动一个新的数据库连接,所以我做了以下更改:
class LoadWorker(QThread):
totalSignal = pyqtSignal(int)
countSignal = pyqtSignal(int)
def __init__(self, parent=None):
super(LoadWorker, self).__init__(parent)
self.threadActive = True
self.commitsize = 200
self.filename = ""
self.database = QSqlDatabase.addDatabase('QSQLITE',"loader")
if self.database:
self.database.setDatabaseName("database/clireports.db")
else:
QMessageBox.critical(None, "Database Error", "Unable To Connect To The Database!")
self.stop()
def run(self):
query = QSqlQuery()
query.exec("DELETE FROM mytable")
#rest of code here
但是,我仍然遇到同样的错误。我需要做些不同的事情吗?
修改代码:
class LoadWorker(QThread):
totalSignal = pyqtSignal(int)
countSignal = pyqtSignal(int)
def __init__(self, parent=None):
super(LoadWorker, self).__init__(parent)
self.threadActive = True
self.commitsize = 200
self.filename = ""
def run(self):
database = QSqlDatabase.addDatabase("QSQLITE","loader")
database.setDatabaseName("database/clireports.db") # <---
if not database.open():
print("Database Error","Unable to connect to the database!")
self.stop()
# database.setDatabaseName("database/clireports.db")
query = QSqlQuery()
query.exec("DELETE FROM mytable")
未编辑的代码:
class LoadWorker(QThread):
totalSignal = pyqtSignal(int)
countSignal = pyqtSignal(int)
def __init__(self, parent=None):
super(LoadWorker, self).__init__(parent)
self.threadActive = True
self.commitsize = 200
self.filename = ""
def run(self):
database = QSqlDatabase.addDatabase("QSQLITE","loader")
database.setDatabaseName("database/clireports.db")
if not database.open():
print("Database Error","Unable to connect to the database!")
self.stop()
query = QSqlQuery()
query.exec("DELETE FROM cptdata")
with open(self.filename, 'r', encoding="ISO-8859-1") as f:
reader = csv.reader(f,delimiter='\t')
data = list(reader)
self.totalSignal.emit(len(data))
f.close()
counter = 0
query.exec_("BEGIN TRANSACTION")
for row in data:
if self.threadActive == False: break
counter +=1
self.countSignal.emit(counter)
if len(row) < 3: continue
if "/" in row[0] or "Code" in row[0]: continue
recordid = str(uuid.uuid1())
cptcode = row[0].strip().upper()
formatcode = cptcode[:5]
description = row[2].strip().upper()
if cptcode == "": continue
query.prepare("INSERT INTO cptdata (recordid,cptcode,description) VALUES(:recordid,:cptcode,:description)")
query.bindValue(":recordid", str(recordid))
query.bindValue(":cptcode", str(formatcode))
query.bindValue(":description", str(description))
if query.exec_():
if counter % 200==0:
database.commit()
query.exec_("BEGIN TRANSACTION")
database.commit()
def stop(self):
self.threadActive = False
self.wait()
class CptLoader(QDialog):
def __init__(self, parent=None):
super(CptLoader, self).__init__(parent)
loadUi("GlobalUI/fileloader.ui", self)
def beginLoad(self,filename):
self.thread = LoadWorker(self)
self.thread.filename = filename
self.thread.totalSignal.connect(self.prgLoader.setMaximum)
self.thread.countSignal.connect(self.prgLoader.setValue)
self.thread.start()