Results 1 to 4 of 4

Thread: Database with multiple thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 11:17. Reason: missing [code] tags

Similar Threads

  1. Multiple connections to MySQL database
    By ShamusVW in forum Newbie
    Replies: 4
    Last Post: 11th August 2010, 06:28
  2. Multiple database connections
    By Matt31415 in forum Qt Programming
    Replies: 9
    Last Post: 4th June 2010, 10:32
  3. Replies: 0
    Last Post: 4th May 2010, 07:49
  4. QSqlDatabase - How to connect to multiple database?
    By cutie.monkey in forum Qt Programming
    Replies: 4
    Last Post: 10th March 2010, 12:03
  5. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16: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
  •  
Qt is a trademark of The Qt Company.