Results 1 to 2 of 2

Thread: UI use QSqltablemodel, the other thread use QSqlQuery to insert , return false

  1. #1
    Join Date
    Mar 2015
    Posts
    1

    Default UI use QSqltablemodel, the other thread use QSqlQuery to insert , return false

    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
    Qt Code:
    1. processDisplayDB =new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE","processDisplaydb"));
    2. processDisplayDB->setDatabaseName("databases/ProductionData.s3db");
    3. if(!processDisplayDB->open()){
    4. qDebug()<<"Could not open file!"<<endl;
    5. return;
    6. }else{
    7. qDebug()<<"Open file: "<<processDisplayDB->databaseName()<<" sucessfully!"<<endl;
    8. }
    9. modelProcessDisplay = new QSqlTableModel(ui->tableViewProcessDisplay,*processDisplayDB);
    10. modelProcessDisplay->setTable("processtable");
    To copy to clipboard, switch view to plain text mode 

    and through a timer to refresh the display
    Qt Code:
    1. connect(&cylicTimerProcessDisplay,&QTimer::timeout,this,&MainWindow::processDisplayUpdate_cylic);
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void MainWindow::processDisplayUpdate_cylic()
    2. {
    3. modelProcessDisplay->select();
    4. }
    To copy to clipboard, switch view to plain text mode 

    the other thread is like this
    Qt Code:
    1. processDB = new QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE","processDB"));
    2. processDB->setDatabaseName("databases/ProductionData.s3db");
    3. if(!processDB->open()){
    4. qDebug()<<"Could not open file!"<<endl;
    5. return;
    6. }else{
    7. qDebug()<<"Open file: "<<processDB->databaseName()<<" sucessfully!"<<endl;
    8. }
    To copy to clipboard, switch view to plain text mode 
    and write data cyclic
    Qt Code:
    1. QSqlQuery query(*processDB);
    2. qDebug()<<"insert processtable prepare : "
    3. <<query.prepare("INSERT INTO processtable(SerialNo) "
    4. "VALUES(:SerialNo)");
    5. query.bindValue(":SerialNo",currentSerialNo);
    6. 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")

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: UI use QSqltablemodel, the other thread use QSqlQuery to insert , return false

    ...and the error returned to the inserting code?
    Ensure the insert code is wrapped in a transaction() that is finished with either commit() or rollback()

Similar Threads

  1. QSqlQuery::exec() returns false all the time
    By ayanda83 in forum Newbie
    Replies: 6
    Last Post: 1st March 2014, 10:59
  2. Why does QSqlQuery::prepare() return false?
    By NotANoob in forum Qt Programming
    Replies: 2
    Last Post: 20th April 2011, 19:05
  3. QWebpage always return false for signal loadfinished
    By QtVenkat in forum Qt Programming
    Replies: 0
    Last Post: 4th June 2010, 20:50
  4. qdbus connect return always FALSE
    By ciberkids in forum Qt Programming
    Replies: 4
    Last Post: 17th September 2009, 14:17
  5. QSqlQuery::isValid returns false
    By mismael85 in forum Qt Programming
    Replies: 24
    Last Post: 7th September 2008, 23:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.