PDA

View Full Version : QSqlQuery doesn't return all results



ce_nort
6th May 2016, 18:48
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:



QSqlQuery query;
query = db.exec("SELECT items.sku, upcs.upc FROM items LEFT JOIN upcs ON items.id = upcs.itemId WHERE items.sku = '1234-XX-XXX'");
while (query.next()) {
qDebug() << query.value("sku").toString();
qDebug() << query.value("upc").toString();
}


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!

ChrisW67
7th May 2016, 00:03
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()?

ce_nort
10th May 2016, 16:45
**Forehead slap** . Thank you, you reminded me that we had a vestigial DB from a previous test. The program was connecting to that.