Hi,
I am struggling with the multi-thread QSQLITE, the GUI thread is refresh the display through QSqlTableModel, the other thread insert data to sqlite cyclically.
but the problem is, once GUI thread use QSqlTableModel::select(), the other thread could not insert , and return : QSqlError("5", "Unable to fetch row", "database is locked")
what could I do next step to resolve the problem?
my codes like below:
this is the GUI thread
processDisplayDB->setDatabaseName("databases/ProductionData.s3db");
if(!processDisplayDB->open()){
qDebug()<<"Could not open file!"<<endl;
return;
}else{
qDebug()<<"Open file: "<<processDisplayDB->databaseName()<<" sucessfully!"<<endl;
}
modelProcessDisplay
= new QSqlTableModel(ui
->tableViewProcessDisplay,
*processDisplayDB
);
modelProcessDisplay->setTable("processtable");
processDisplayDB =new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE","processDisplaydb"));
processDisplayDB->setDatabaseName("databases/ProductionData.s3db");
if(!processDisplayDB->open()){
qDebug()<<"Could not open file!"<<endl;
return;
}else{
qDebug()<<"Open file: "<<processDisplayDB->databaseName()<<" sucessfully!"<<endl;
}
modelProcessDisplay = new QSqlTableModel(ui->tableViewProcessDisplay,*processDisplayDB);
modelProcessDisplay->setTable("processtable");
To copy to clipboard, switch view to plain text mode
and through a timer to refresh the display
connect(&cylicTimerProcessDisplay,&QTimer::timeout,this,&MainWindow::processDisplayUpdate_cylic);
connect(&cylicTimerProcessDisplay,&QTimer::timeout,this,&MainWindow::processDisplayUpdate_cylic);
To copy to clipboard, switch view to plain text mode
void MainWindow::processDisplayUpdate_cylic()
{
modelProcessDisplay->select();
}
void MainWindow::processDisplayUpdate_cylic()
{
modelProcessDisplay->select();
}
To copy to clipboard, switch view to plain text mode
the other thread is like this
processDB->setDatabaseName("databases/ProductionData.s3db");
if(!processDB->open()){
qDebug()<<"Could not open file!"<<endl;
return;
}else{
qDebug()<<"Open file: "<<processDB->databaseName()<<" sucessfully!"<<endl;
}
processDB = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE","processDB"));
processDB->setDatabaseName("databases/ProductionData.s3db");
if(!processDB->open()){
qDebug()<<"Could not open file!"<<endl;
return;
}else{
qDebug()<<"Open file: "<<processDB->databaseName()<<" sucessfully!"<<endl;
}
To copy to clipboard, switch view to plain text mode
and write data cyclic
qDebug()<<"insert processtable prepare : "
<<query.prepare("INSERT INTO processtable(SerialNo) "
"VALUES(:SerialNo)");
query.bindValue(":SerialNo",currentSerialNo);
qDebug()<<"insert processtable: "<<query.exec();
QSqlQuery query(*processDB);
qDebug()<<"insert processtable prepare : "
<<query.prepare("INSERT INTO processtable(SerialNo) "
"VALUES(:SerialNo)");
query.bindValue(":SerialNo",currentSerialNo);
qDebug()<<"insert processtable: "<<query.exec();
To copy to clipboard, switch view to plain text mode
but the query.exec() return false, once the GUI thread excuted the select(), the return error is : QSqlError("5", "Unable to fetch row", "database is locked")
Bookmarks