Results 1 to 3 of 3

Thread: QSqlQuery doesn't return all results

  1. #1
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default QSqlQuery doesn't return all results

    What I am trying to achieve: I am trying to get all UPCs associated with a given sku from our database.

    The problem: I have a SQL query that should return multiple results for a sku value in a WHERE clause, and when run in a database management application it works as expected. However, QSqlQuery only returns the first result. Essentially, it seems that it only returns results if both values are unique (or something). Below is some simplified code:

    Qt Code:
    1. QSqlQuery query;
    2. query = db.exec("SELECT items.sku, upcs.upc FROM items LEFT JOIN upcs ON items.id = upcs.itemId WHERE items.sku = '1234-XX-XXX'");
    3. while (query.next()) {
    4. qDebug() << query.value("sku").toString();
    5. qDebug() << query.value("upc").toString();
    6. }
    To copy to clipboard, switch view to plain text mode 

    When the query is run in a db manager, it returns:
    ('1234-XX-XXX', [upc1]) // sku and first associated upc
    ('1234-XX-XXX', [upc2]) // sku and second associated upc
    ('1234-XX-XXX', [upc3]) // sku and third associated upc

    But the QSqlQuery only returns the sku and first associated upc:
    "1234-XX-XXX"
    "[upc1]"

    I have tried multiple different ways of preparing and executing the query, and multiple different ways of checking the result/record. Every time, only the first entry is returned, even though three entries are returned by the exact same query in a db manager. Am I doing something wrong? Is this something related to exactly how QSqlQuery works? Any help would be much appreciated!

  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: QSqlQuery doesn't return all results

    Absolutely certain you are connecting to the same database?
    Have you tried a direct query from your code on upcs with the right itemId explicitly specified to see there are three records in that table?
    Have you checked QSqlQuery::lastError()?

  3. #3
    Join Date
    Nov 2015
    Location
    Vermont
    Posts
    52
    Thanks
    15
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    MacOS X Windows

    Default Re: QSqlQuery doesn't return all results

    **Forehead slap** . Thank you, you reminded me that we had a vestigial DB from a previous test. The program was connecting to that.

Similar Threads

  1. QSqlQuery: results size are limited to 4095 bytes
    By breizhim in forum Qt Programming
    Replies: 6
    Last Post: 3rd March 2011, 22:12
  2. How to delete results of QSqlQuery from table ?
    By homerun4711 in forum Newbie
    Replies: 2
    Last Post: 14th February 2011, 22:50
  3. QSqlQuery don't return
    By ShadowBelmolve in forum Newbie
    Replies: 2
    Last Post: 9th September 2010, 20:28
  4. Extracting QSqlQuery results in PyQt4
    By amicitas in forum Qt Programming
    Replies: 2
    Last Post: 9th October 2008, 05:10
  5. Can't get QXmlQuery to return results
    By bpetty in forum Newbie
    Replies: 2
    Last Post: 13th May 2008, 14:27

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.