8 个解决方案
#1
没人看见么?
#2
不知道是不是跟垃圾回收不及时有关。你的调用频率很高么?
#3
不高的,在一个函数中有两处sql调用,第一个是主线程直接用sqlquery去查询,第二个是子线程中有使用sqlquery查询。要是主线程调用放在前面,出错的概率小了很多。要是子线程的放在前面先调用那么就特别容易崩(程序是全屏的,当切换到其他程序之后再切换回来,再调用就容易崩)。不知道是不是线程访问的问题。
#4
不同的线程使用不同的QSqlDatabase对象(connection name 不一样)。
#5
不同的线程不能共用一个QSqlDatabase对象!这个Qt的官方技术文档上有明确说明:
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information
详细请看thread-support in Qt Modules
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information
详细请看thread-support in Qt Modules
#6
那就是在每个子线程中也要有数据库驱动加载、设置hostName、databaseName、userName、password的操作。那我来测试一下。先谢谢啦!
#7
嗯,谢谢了。我已经将该部分浏览了一遍,准备着手去修改此部分代码。
#8
嗯,刚才测试过了。
db = QSqlDatabase::addDatabase("QMYSQL","maindatabase");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("580231");
if(db.open()){
QSqlQuery query(db); //此时在构建QSqlQuery对象时传入自定义的db,否则QSqlQuery无法使用
query.prepare("select * from user");
if(query.exec()){
while(query.next()){
qDebug()<<"id:"<<query.value(0).toInt()<<"_name:"<<query.value(1).toString();
}
}
}
#1
没人看见么?
#2
不知道是不是跟垃圾回收不及时有关。你的调用频率很高么?
#3
不高的,在一个函数中有两处sql调用,第一个是主线程直接用sqlquery去查询,第二个是子线程中有使用sqlquery查询。要是主线程调用放在前面,出错的概率小了很多。要是子线程的放在前面先调用那么就特别容易崩(程序是全屏的,当切换到其他程序之后再切换回来,再调用就容易崩)。不知道是不是线程访问的问题。
#4
不同的线程使用不同的QSqlDatabase对象(connection name 不一样)。
#5
不同的线程不能共用一个QSqlDatabase对象!这个Qt的官方技术文档上有明确说明:
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information
详细请看thread-support in Qt Modules
A connection can only be used from within the thread that created it. Moving connections between threads or creating queries from a different thread is not supported.
In addition, the third party libraries used by the QSqlDrivers can impose further restrictions on using the SQL Module in a multithreaded program. Consult the manual of your database client for more information
详细请看thread-support in Qt Modules
#6
那就是在每个子线程中也要有数据库驱动加载、设置hostName、databaseName、userName、password的操作。那我来测试一下。先谢谢啦!
#7
嗯,谢谢了。我已经将该部分浏览了一遍,准备着手去修改此部分代码。
#8
嗯,刚才测试过了。
db = QSqlDatabase::addDatabase("QMYSQL","maindatabase");
db.setHostName("localhost");
db.setDatabaseName("test");
db.setUserName("root");
db.setPassword("580231");
if(db.open()){
QSqlQuery query(db); //此时在构建QSqlQuery对象时传入自定义的db,否则QSqlQuery无法使用
query.prepare("select * from user");
if(query.exec()){
while(query.next()){
qDebug()<<"id:"<<query.value(0).toInt()<<"_name:"<<query.value(1).toString();
}
}
}