Results 1 to 4 of 4

Thread: Database with multiple thread

  1. #1
    Join Date
    Jun 2015
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Question Database with multiple thread

    Hi,
    I want to access the database from 2 different threads and i am establishing the connection of database within the threads, but still when i access dartabase i get error as follows:
    QSqlQuery:repare: database not open

    My code snippet is as follows:
    In constructor i am starting the 2 threads

    Qt Code:
    1. pthread_create(&hd1, NULL, a, NULL);
    2. pthread_create(&hd2, NULL, b, NULL);
    3. pthread_join(hd1,NULL);
    4. pthread_join(hd2,NULL);
    To copy to clipboard, switch view to plain text mode 

    The below are Thread functions:
    Qt Code:
    1. void *a(void *arg)
    2. {
    3. db = QSqlDatabase::addDatabase("QPSQL","con1");
    4. db.setDatabaseName("abc");
    5. db.setHostName("127.0.0.1");
    6. db.setUserName("root");
    7. db.setPassword("123456");
    8. db.open();
    9.  
    10. while(1)
    11. {
    12. printf("In Thd1\n");
    13. QSqlQuery qry2;
    14. qry2.prepare("SELECT max(msgid) from tx_msg");
    15. qry2.exec();
    16. while(qry2.next())
    17. {
    18. printf("MSGID:%d",qry2.value(0).toInt()) ;
    19. }
    20. usleep(1000);
    21. }
    22. pthread_exit(NULL);
    23. }
    To copy to clipboard, switch view to plain text mode 

    2nd thread :

    Qt Code:
    1. void *b(void *arg)
    2. {
    3. db1 = QSqlDatabase::addDatabase("QPSQL","con2");
    4. db1.setDatabaseName("abc");
    5. db1.setHostName("127.0.0.1");
    6. db1.setUserName("root");
    7. db1.setPassword("123456");
    8. db1.open();
    9. while(1)
    10. {
    11. printf("In Thd2\n");
    12. QSqlQuery qry1;
    13. qry1.prepare("SELECT max(msgid) from tx_msg");
    14. qry1.exec();
    15. while(qry1.next())
    16. {
    17. printf("MSGID:%d",qry1.value(0).toInt()) ;
    18. }
    19. usleep(1000);
    20. }
    21. pthread_exit(NULL);
    22. }
    To copy to clipboard, switch view to plain text mode 

    How can i access same database from different threads?
    Last edited by Lykurg; 9th June 2015 at 12:17. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Database with multiple thread

    First : QSqlDatabase::open returns bool value - test it.
    Second : if open() == false use QSqlDatabase::lastError() to check what is wrong.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Database with multiple thread

    QSqlQuery takes an argument of the database handler it is to operate on. If one is not given, it falls back to the default connection which obviously in your case is not open.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2015
    Posts
    8
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Database with multiple thread

    Thank You,
    It's working fine.
    I have added database handler to QSqlQuery.

Similar Threads

  1. Multiple connections to MySQL database
    By ShamusVW in forum Newbie
    Replies: 4
    Last Post: 11th August 2010, 07:28
  2. Multiple database connections
    By Matt31415 in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2010, 11:32
  3. Replies: 0
    Last Post: 4th May 2010, 08:49
  4. QSqlDatabase - How to connect to multiple database?
    By cutie.monkey in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 13:03
  5. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 17:56

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.