0

In my app in QT5 I have this code

QString sql = "Select * from table";
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("./au.sqlite");
db.open();
QSqlQuery query(sql);
query.exec();

But when I get the results only get one result, the select query only give me one result and I don't know how fix this. If I add query.next() in a while loop I only get one iteration.

Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313
  • 1
    Please make a self-contained test case that initializes an in-memory database with a table and demonstrates the problem. See [this answer](http://stackoverflow.com/a/11064135/1329652) or [that answer](http://stackoverflow.com/a/11021486/1329652) for inspiration in that matter. – Kuba hasn't forgotten Monica Jun 26 '15 at 16:49

2 Answers2

0

You might check sqlbrowser example that is included with Qt5 distribution. Run your query there on your db.

Cristea Bogdan
  • 116
  • 1
  • 11
0

There is a bool function that returns true if the query succeeded. Looking back to the docs:

Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive.

This is the way you can check the result. If it's false, then You should try to rewrite your sql query text so that functional sql words (such as SELECT, FROM and etc.) are in the upper case (the way it's shown in the docs) and try to execute the query once again.

Here's an example from the docs again:

QSqlQuery query("SELECT country FROM artist");
while (query.next()) {
    QString country = query.value(0).toString();
    doSomething(country);
}
dazewell
  • 396
  • 2
  • 17