PDA

View Full Version : qt how to connect to sqlite db file



TomASS
22nd April 2009, 00:11
Hello, I have a sqlite db file, it name is "tomek.sqlite". In this db I have a one table name: "am_tankowania"

In code, I put:

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

db.setDatabaseName("tomek.sqlite");
if(db.open()) QMessageBox::information(0,"Ok","Connection ok");
else QMessageBox::information(0,"Error","some error.");

The MessageBox ok - connection ok show. Now, I have a query:

select * from am_tankowania:


QSqlQuery query("SELECT * am_tankowania");
if (query.lastError().isValid()) QMessageBox::critical(0,"Error",query.lastError().text() );


I see an error:
no such table table: am_tankowania Unable to execute statement :/

What i'm doing wrong?

Thank you.

lyuts
22nd April 2009, 09:42
Well, this might be because db.setDatabaseName("tomek.sqlite"); points to a missing db file called tomek.sqlite. This should be a relative path. The reason is that probably your db file is not in the same directory as your executable.



The MessageBox ok - connection ok show.


It will always show ok. Because if there is no such file it will create it and open it (empty though).

spirit
22nd April 2009, 09:47
this query looks wrong


QSqlQuery query("SELECT * am_tankowania");

change it to


QSqlQuery query("SELECT * FROM am_tankowania");