6

I try to deal with SQLite database on Qt 4.5.3 on Linux. I've already created the databsae.

Then, I try to perform select on Qt:

db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(filename); // Here is FULL path to the database. I've checked it twice :)
bool ok = db.open();
qDebug() << db.tables();

QSqlQuery query;
query.exec("select * from lessons");
qDebug() << query.size();
qDebug() << query.isSelect();
qDebug() << query.isValid();

But debug console says:

("lessons", "weeklessons", "weeks") 
-1 
true 
false 

Why it's select nothing? What I have doing wrong?

1
  • 4
    It seems that .size() doesn't work with Sqlite :( That is why You have -1. Commented Mar 23, 2011 at 20:33

5 Answers 5

9

The isValid() method returns true if the query is positionned on a valid record, but after calling exec(), it isn't : you have to move to a valid record first, for example with query.first() or query.next(). See Qt docs : http://doc.qt.io/archives/4.6/qsqlquery.html

The size() returning -1 doesn't mean there is no result : SQLite is one of the databases for which the size of the query is not directly available (look in the documentation for QSqlDriver::hasFeature()). You can check that rows are returned and find the size with a loop and query.next().

Depending on what you want to do with the result of your select, you could also use QSqlQueryModel instead of QSqlQuery.

Sign up to request clarification or add additional context in comments.

1 Comment

That looks like the issue. @vas3k in general you should check the return value of exec() to see wether the execution of the query succeeded.
3

In your "QSqlQuery query;" declaration, you have to specify the database connection, e.g. "QSqlQuery query(db)"

Comments

1

Given that your program reports that the query is invalid, have a look at the error message as follows:

QDebug() << query.lastError().text();

This should help you in debugging the problem.

4 Comments

Thanx for the answer, yes I know. But it output an empty string (just one space): ("lessons", "weeklessons", "weeks") " " -1
Are you sure the query actually works? Try it from the SQLite prompt. Maybe adding a semi-colon at the end of your query might help? Finally, I would try getting the SQLite example that is shipped with Qt to work.
Yes it works in my SQLite Viewer: lh3.ggpht.com/_hyIyDtqe5N0/S4KNBpfapeI/AAAAAAAAAU0/3MEliEIy8G4/… Adding semicolon didn't helps too. And where can I find Qt SQLite examples? I found only MySQL.
This one for example: qt.nokia.com/doc/4.6/sql-sqlwidgetmapper.html It uses an in memory SQLite database, but it should be easy to rewrite the example to use an SQLite database from disk.
0

This can give you a answer: http://www.qtcentre.org/threads/7904-Is-there-any-other-reason-why-QSqlQuery-size%28%29-returns-1

1 Comment

Answers here are considered best if they don't only provide a link (however helpful that may be) but also explain the reasons of the problem or describe a solution. You can always edit your answers to improve them.
0

Without knowing much about Qt, I was asking myself one question: How does the QSqlQuery know which connection to use. Looking that up in the manual revealed that there are basically two possibilities to execute a query:

  1. query = db.exec("select * from lessons");
    Using the database connection, and executing from there.
  2. QSqlQuery query( db );
    Constructing the query using the existing database and then executing the query:
    query.exec("select * from lessons");

edit: After reading a bit more, it seems that default connections are supported, so your example should work…

Could you try passing the query as the constructor's value instead? Maybe it works then.

QSqlQuery query("select * from lessons");

2 Comments

Qt makes it as "default connection". Yes, of course I tried to do this way. Nope, same errors. db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName(filename); bool ok = db.open(); qDebug() << db.tables(); QSqlQuery query = db.exec("select * from lessons"); qDebug() << query.size(); output: ("lessons", "weeklessons", "weeks") -1
And I tried to add query to constructor's value. Same errors, don't work too. Very strange :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.