Results 1 to 2 of 2

Thread: Multiple Queries populating QSqlQueryModel

  1. #1
    Join Date
    Feb 2012
    Location
    New Orleans
    Posts
    6
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Multiple Queries populating QSqlQueryModel

    I realize my problem would be solved by creating a view in the database that I could set to my QSqlQueryModel. Unfortunately, I do not have the permissions to create a view. I also realize my problem could be solved by simply creating a longer SELECT query; unfortunately, the database (disgustingly inefficient design) cannot handle the length of query I need to execute.

    My solution is to create multiple queries that will run one at a time. Then I would like to use QSqlQueryModel to put all the information together and in a QTableView. Is this possible? Can I use more than one query in the QSqlQueryModel?

    Here is the code for one query that I know works. I just have 8 more queries like it that need to be in the same table:

    Qt Code:
    1. model->setData ("SELECT props.number, props.sample, props.top, props.bottom, AVG(ds.magn), MEDIAN(TO_NUMBER(ds.magn)) FROM ds JOIN props ON props.number = ds.number WHERE ds.cd BETWEEN props.top AND props.bottom GROUP BY props.number, props.cnumber, props.sample, props.top, props.bottom HAVING props.number = 11 ORDER BY TO_NUMBER(props.cnumber) desc, TO_NUMBER(props.sample) desc");
    2. if (model->lastError().isValid())
    3. qDebug() << model->lastError();
    4. else qDebug() << "success!";
    5. model->setHeaderData(0, Qt::Horizontal, QObject::tr("CNumber"));
    6. model->setHeaderData(1, Qt::Horizontal, QObject::tr("Sample"));
    7. model->setHeaderData(2, Qt::Horizontal, QObject::tr("Top"));
    8. model->setHeaderData(3, Qt::Horizontal, QObject::tr("Bottom"));
    9. model->setHeaderData(4, Qt::Horizontal, QObject::tr("Mean Magnectic"));
    10. model->setHeaderData(5, Qt::Horizontal, QObject::tr("Median Magnectic"));
    11.  
    12. QTableView *view = new QTableView;
    13. view->setModel(model);
    14. view->show();
    To copy to clipboard, switch view to plain text mode 

    Thank you for any assistance!

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Multiple Queries populating QSqlQueryModel

    Can I use more than one query in the QSqlQueryModel?
    No. One SELECT query returning one result set. At this point your query becomes one of pure SQL.

    the "HAVING props.number = 11" clause should be in the "WHERE". "Having" is for placing conditions on the results of aggregate functions after grouping.

    When you say you have "8 more queries like it that need to be in the same table" do you mean that these other queries output the same columns and their results just need to be appended? If so, a UNION or UNION ALL query is another approach.

    Are all the queries varying only the props.number of interest? If so then "WHERE props.number in (11, 23, 45, 89 ...)" might help.

Similar Threads

  1. Odd behavior when populating a Table
    By Kyle_Katarn in forum Newbie
    Replies: 8
    Last Post: 8th April 2011, 22:38
  2. Multiple sql queries
    By fantom in forum Qt Programming
    Replies: 3
    Last Post: 28th February 2011, 17:00
  3. Populating, signals and comboboxes
    By ShamusVW in forum Newbie
    Replies: 6
    Last Post: 12th August 2010, 06:43
  4. Replies: 9
    Last Post: 19th November 2009, 09:18
  5. QTableView populating question
    By onefootswill in forum Newbie
    Replies: 8
    Last Post: 7th August 2008, 22:29

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.