Results 1 to 3 of 3

Thread: Two connections from two threads to one database

  1. #1
    Join Date
    Jul 2019
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Two connections from two threads to one database

    For a few days I'm still struggling to make this code work. I have one database (SQLITE), to which I connect at the beginning of the program.
    Qt Code:
    1. QString path: "xxxxx";
    2. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    3. db.setDatabaseName(path);
    4. if(db.open())
    5. qDebug()<<"database"<<path<<"opened";
    6. else {
    7. qDebug()<<"unable to open database"<<path;
    8. }
    9.  
    10. /* FOR THE SAKE OF EXAMPLE
    11.   QSqlQuery query;
    12.   bool result=query.exec("CREATE TABLE IF NOT EXISTS compdefs "
    13.   "(id INTEGER PRIMARY KEY AUTOINCREMENT, "
    14.   "name VARCHAR(20), "
    15.   "minarea INTEGER, "
    16.   "aspectratio DOUBLE,"
    17.   "orientationmode INTEGER,"
    18.   "upsample INTEGER,"
    19.   "horizontalflip BOOLEAN NOT NULL CHECK (horizontalflip IN (0,1)),"
    20.   "verticalflip BOOLEAN NOT NULL CHECK (verticalflip IN (0,1)),"
    21.   "c DOUBLE,"
    22.   "eps DOUBLE,"
    23.   "numthreads INTEGER,"
    24.   "trainedfhog BOOLEAN NOT NULL DEFAULT 0,");
    25.  
    26. */
    To copy to clipboard, switch view to plain text mode 

    Everything work fine, I can add, or remove from database as I want. But, in one moment, I need to push a button, this button create a QtConcurentRun, which call another thread to do my training HOG classifier function, and my main thread keep my GUI unfreeze!

    But when my train HOG classifier function is complete, i need to save those data in my sqlite database, which is in my main thread and when I try to save those data, I get this error:
    Qt Code:
    1. QSqlDatabasePrivate::database: requested database does not belong to the calling thread.
    To copy to clipboard, switch view to plain text mode 
    Is any method to update my database from the second thread? Maybe create a second connection? How I do this? If I create a second connection I need to remove fisrt connection? Please, I need some help.

    This is my part of code, when I push the button.

    Qt Code:
    1. void CompDefDialog::on_fhogTrainBtn_clicked(){
    2. //other stuff
    3. trainHOGFuture = QtConcurrent::run(compDef, &CompDef::trainFHOG);
    4. }
    To copy to clipboard, switch view to plain text mode 


    and this is my trahinFHOG function who start in the second thread:


    Qt Code:
    1. void CompDef::trainFHOG(void)
    2. {
    3. QElapsedTimer timer;
    4. timer.start();
    5. //other stuff
    6. // from this line, I start to save my new data in my database
    7. if(id<=0)
    8. {
    9. qDebug()<<"save component definition first";
    10. return;
    11. }
    12.  
    13. trainedFHOG=true;
    14. oldFHOG=false;
    15.  
    16. QSqlQuery query;
    17. std::ostringstream buf;
    18. dlib::serialize(fhogDetector, buf);
    19.  
    20. QString sql="UPDATE compdefs SET fhogclassifier= :classifier, "
    21. "trainedfhog= :trainedfhog, "
    22. "oldfhog= :oldfhog "
    23. "WHERE id=:id";
    24.  
    25. query.prepare(sql);
    26. query.bindValue(":classifier", QByteArray(buf.str().c_str(),buf.str().length()));
    27. query.bindValue(":id", id);
    28. query.bindValue(":trainedfhog", trainedFHOG);
    29. query.bindValue(":oldfhog", oldFHOG);
    30.  
    31. if(!query.exec())
    32. {
    33. qDebug()<<"error while saving fhog classifier:"<<query.lastError()<<query.lastQuery();
    34. qDebug() << QSqlDatabase::drivers();
    35. }
    36. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two connections from two threads to one database

    Hi,

    When the thread execution finish the training process you can emit a signal to the mainThread and let the mainThread get the object data to store it into the database. You have to let the mainThread access to the CompDef members data via method.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Two connections from two threads to one database

    Couple of options

    1) Store the data in a member variable of the class, of course with proper locking and handle the database query in the main thread once the function has finished

    2) Return the data from the trainFHOG() function, retrieve from the future after completion and do the database interaction in the main thread

    3) As ^NyAw^ suggested emitting the data from the trainFHOG() using a signal, then do the database interaction in the main thread

    4) Create a new database connection inside the trainFHOG() function using QSqlDataBase::addDatabase() with different name than the one used on the main thread.
    Also obviously calling removeDatabase() before returning.

    Cheers,
    _

Similar Threads

  1. Replies: 1
    Last Post: 9th January 2012, 01:23
  2. Removing database connections
    By Matt31415 in forum Qt Programming
    Replies: 1
    Last Post: 4th July 2010, 08:08
  3. Multiple database connections
    By Matt31415 in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2010, 11:32
  4. Qt Mem Leak/growth with queued connections across threads?
    By chuckshaw in forum Qt Programming
    Replies: 8
    Last Post: 16th August 2008, 17:43
  5. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 17:56

Tags for this Thread

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.