QSqlDatabasePrivate::database: requested database does not belong to the calling thread.

连接数据库的名字不可以使用一个,创建一个随机数,保证不重复

https://forum.qt.io/topic/113422/requested-database-does-not-belong-to-the-calling-thread

https://blog.csdn.net/shawzg/article/details/96484966

'''

db_mutex = QMutex()

def connectdb():
    
    db_mutex.lock() 
    
    db =  QSqlDatabase.addDatabase('QSQLITE',str(random.random()))
    
    db.setDatabaseName("db/database.db")
    db_mutex.unlock()
    
    if not db.open():
        mess = "打开数据库 database.db 错误:\n" + db.lastError().text()
        mbox = Messbox(mess)
        globalv.gl_log_db.info(mess)
        return None
    
    return db

'''
Logo

一站式 AI 云服务平台

更多推荐