Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSqlQuery bug? Prepared statement with LIKE and wildcards
Zerby last edited by
I'm pretty sure I've found a bug in QSqlQuery, when using a prepared statement with LIKE, and binding a value to it with double wildcards (%). The first execution of the query returns proper results, but the second one always fails to return anything, eventhough the value bindings are idenctical.
std::cout << "PREPARE: " << testQuery.prepare("SELECT product_id FROM products WHERE name LIKE :name") << std::endl;
testQuery.bindValue(":name", "%Test%"); std::cout << "EXEC: " << testQuery.exec(); std::cout << " NEXT: " << testQuery.next() << std::endl; testQuery.bindValue(":name", "%Test%"); std::cout << "EXEC: " << testQuery.exec(); std::cout << " NEXT: " << testQuery.next() << std::endl;
This always returns:
EXEC: 1 NEXT: 1
EXEC: 1 NEXT: 0
Which shows that the prepare goes fine, and both times the exec call is successfull, but the second time no results are fetched, even though the queries are identical! Even if you comment out the second bindValue, the second exec call still returns no rows.
EDIT: Also, if I run the code with no wildcards (%), both times the next() call returns true, so it seems indeed QSqlQuery has a bug when using double wildcards with bindValue.