PDA

View Full Version : SQLite doesnt return data



HelderC
27th May 2011, 04:28
I'm using Qt + Sqlite3 at Kubuntu 11.04.

When i execute the program from QtCreator or Shell, it runs ok.
The problem is when i try execute it (compiled) by double-click. It can not receive the data from database file. Its connect with the database file but doesnt return the data.

I'm using this to connect:
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("dataBase.db");
db.open();


This works fine.

But when i run this, its return nothing:
QSqlQuery resultado = db.exec("select * from pessoas");
while (resultado.next())
ui->plainTextEdit->appendPlainText(resultado.value(0).toString() +
" - " + resultado.value(1).toString());

Any idea about what is happening?

ChrisW67
27th May 2011, 05:21
No, but I'm sure if you look at the return value from QSqlDataBase::open() and the state of the QSqlQuery returned from QSqlDatabase::exec() you would be given a clue. You should be looking at QSqlQuery::isActive(), QSqlDatabase::lastError() and/or QSqlQuery::lastError().

As the docs say,
QSqlDatabase::exec() is deprecated. Use QSqlQuery::exec() instead.

You also wanted
tags rather than [qtclass]

DanH
27th May 2011, 12:24
Probably the database record is blank. Probably because you're executing in a different environment and the database is not where it ought to be.

Display QFileInfo("dataBase.db").absoluteFilePath() to see where the app is looking for the database.

HelderC
27th May 2011, 19:25
Probably the database record is blank. Probably because you're executing in a different environment and the database is not where it ought to be.

Display QFileInfo("dataBase.db").absoluteFilePath() to see where the app is looking for the database.

The app is looking for the db, everything is ok about this. When a run the app at QtCreator, i can to get the records.
The QSqlDataBase::open() return true, the connection is working...

DanH
27th May 2011, 21:14
You said when you "execute it (compiled) by double-click" you have problems. When you do that, you're very likely getting a different default directory, so the DB is not where the program's looking for it.

Do as I said and display the path where the code is looking for the DB.

HelderC
29th May 2011, 02:40
I got solve the problem he was having.
Strangely, even notifying that it was connected to my application could not find the path to the SQLite file that was in the same folder as the application executable.
To resolve this I used the following code:


db.setDatabaseName(QApplication::applicationDirPat h() +
QDir::separator() + "dataBase.db");

DanH
29th May 2011, 12:56
Like I said, you were looking in the wrong place.