Results 1 to 3 of 3

Thread: How to select one table with QSqlTableModel (QSqlQuery or ...) in multithreading

  1. #1
    Join Date
    Jan 2014
    Posts
    20
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to select one table with QSqlTableModel (QSqlQuery or ...) in multithreading

    Hi,
    To select all the rows(around 20000 rows) from table in the Oracle database, i use QSqlTableModel like this:
    Qt Code:
    1. model.setTable(tableA);
    2. model.select();
    3.  
    4. while (model.canFetchMore())
    5. {
    6. model.fetchMore();
    7. }
    8.  
    9. int nRowCount = model.rowCount();
    10.  
    11. for(int i = 0; i<nRowCount; i++)
    12. {
    13. //handle each row
    14. //..
    15. //..
    16. }
    To copy to clipboard, switch view to plain text mode 

    But using canFetchMore() and fetchMore() in a while loop to get all the rows need too much time, so i refer to this article http://www.qtcentre.org/threads/1263...-speed-problem. Then i plan to start serval threads in MyModel(inherits RemoteTableModel as illustrated in article), each thread start to select part of table and add the part into MyModel, but i am not sure how to do it in details, could i use QSqlTableModel to select in each thread?
    Could you give me some tips or any better way?
    Thanks!

  2. #2
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: How to select one table with QSqlTableModel (QSqlQuery or ...) in multithreading

    You have to investigate your driver. Not all drivers support multi-threads. Some do. I think Oracle's does, but don't quote me on that. In addition Qt has threading limitations with drivers too. You'll probably need a connection per thread (see QSqlDatabase::addDatabase(). Note when I was connecting to oracle, the oracle connect time took a while.

    Once you've got all that sorted out, you can just use Qt primitive types like QMap Or QList to share data between threads.

    However I think there is a better approach overall. Why do you need all 20k rows at once? Either have the database do the work, or if displaying them, let a ModelView handle displaying them, which also handles loading.

  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: How to select one table with QSqlTableModel (QSqlQuery or ...) in multithreading

    Quote Originally Posted by silentyears View Post
    could i use QSqlTableModel to select in each thread?
    Probably, but that would be an unnecessary overhead given that you do not need a model in the thread.
    Just run QSqlQuery and notify the model of data as it comes in.

    It is not very wide to split your problem over so many threads, because anyone not following it through all your threads will not have the overview and always concentrate on what your most current thread asks for instead of helping you solve the problem.

    Do you expect all readers of your threads to click on your name and research which postings you might have already made on the topic?

    Cheers,
    _

Similar Threads

  1. QSqlTableModel select() / submitAll() very slow
    By mihaibu in forum Qt Programming
    Replies: 1
    Last Post: 4th August 2013, 17:43
  2. QSqlQuery bindValue and SELECT WHERE IN
    By helloworld in forum Qt Programming
    Replies: 5
    Last Post: 6th November 2010, 16:45
  3. QSqlQuery problem with SELECT statement
    By virtuosite in forum Newbie
    Replies: 2
    Last Post: 31st August 2009, 09:13
  4. QSqlQuery and QSqlTableModel
    By baray98 in forum Qt Programming
    Replies: 0
    Last Post: 17th July 2009, 18:44
  5. QSqlTableModel::select()
    By vladozar in forum Qt Programming
    Replies: 4
    Last Post: 29th April 2009, 18:51

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.