Results 1 to 3 of 3

Thread: Database queries and Threads

  1. #1
    Join Date
    May 2011
    Posts
    30
    Thanks
    1

    Default Re: Database queries and Threads

    I need to write a multithreaded application doing database queries.
    At this time I have chosen the following architecture:

    Qt Code:
    1. DatabaseTools : public QObject
    2. // wrapper class that provides programmer with an abstracted functions such as "connect" and "login"
    3. {
    4. DatabaseObject : public QThread, public QSqlDatabase
    5. // provides programmer with multithreaded connect in "void run ()"
    6.  
    7. DatabaseQuery : public QThread, QSqlQuery
    8. DatabaseQuery (DatabaseObject*, QString query);
    9. // provides programmer with multithreaded database query in "void run ()"
    10. }
    To copy to clipboard, switch view to plain text mode 

    When I use connect method of DatabaseTools (opens database connection with multithreaded QSqlDatabase::open in DatabaseObject::run()) everything works great, but when I try to do some multithreaded queries, QSqlQuery's exec() method returns false with a simple query. There is also a problem with last error (displays nothing).

    I have read that there are some peculiarities with Qt Sql Module and threads.

    Can you advise me a good achitecture of multithreaded database queries?


    Added after 8 minutes:


    I've been struck with a perfect idea, I can write multithreaded sql qeuries with a database sdk, that means that I can use database sdk in Qt classes. But I have to write another module if I change database from MySQL to PostgreSql, for example.

    At the same moment, could you suggest a good architecture in the post above?
    Last edited by zzz9; 9th August 2012 at 09:42.

  2. #2
    Join Date
    Jul 2012
    Posts
    40
    Thanks
    12
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Database queries and Threads

    You have to establish the database connection in run Method of Thread:

    Look at this article http://www.linuxjournal.com/article/9602
    Pursue your targets and don't give up....

  3. #3

    Default Re: Database queries and Threads

    So we have this ultimate fail vendor (it's a long story, but trust me here) that has created an application that has separate, but identical in design, databases that we need to query (10 of them actually). Each of these databases is for a different "location" - but it's all still information relevant to all locations.

    I've written some code to start 10 unique threads to issue queries to each of the separate databases, wait for the results to finish, and then combine them into a single set of records that can be used an manipulated.

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 09:30
  2. MySql Database and Threads
    By moosa in forum Qt Programming
    Replies: 1
    Last Post: 2nd December 2010, 13:00
  3. Qt Basic Queries
    By somnathbanik in forum Newbie
    Replies: 6
    Last Post: 2nd November 2010, 11:28
  4. QSQLITE database can't exec queries
    By mcwar in forum Qt Programming
    Replies: 5
    Last Post: 13th January 2010, 00:47
  5. Replies: 9
    Last Post: 19th November 2009, 10:18

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.