Solved The SQLite exception "no such table: <table> Unable to execute statement" with QSqlDatabase
-
@Maarouf-Med-Mahdi
I'm sure there is such database in correct path. But did you found another file with the same name in other directory?
Also, you should use '/' as directory separator:Qt uses "/" as a universal directory separator in the same way that "/" is used as a path separator in URLs. If you always use "/" as a directory separator, Qt will translate your paths to conform to the underlying operating system.
-
@Maarouf-Med-Mahdi
With SQLiteStudio you are connecting to real database and everything works.
With Qt you are trying to connect to wrong database and new database file is created and connected. This database is empty and that's why there is no tables in it.
Execute this query with SQLiteStudio and with Qt and compare the results.select * from sqlite_master where tbl_name = 'serveurs';
And how much files with name "GestionDataCenter_BD.db" did you found in your computer?
-
I copied the same database (exported) and put it in the directory of my project. and I checked with notepade that all the tables are created, in the end I gave it to QT by the way of this last database
-
@Stoyan that query
select * from sqlite_master where tbl_name = 'serveurs';
works in the both -
@Maarouf-Med-Mahdi
It always works. The question is how many rows returns in each case. -
@Stoyan -1
QString text_req = "select * from sqlite_master where tbl_name='serveurs'";
if(requette.exec(text_req))
qDebug()<<"requette valide"<<requette.size();
else
qDebug()<<"invalide requette"<<" --> "<<requette.lastError().text();
connected
requette valide -1 -
@Maarouf-Med-Mahdi
Unfortunately, SQLite doesn't support QSqlQuery::size() function.
Instead you can use this:if(requette.exec(text_req)) { requette.last(); qDebug() << requette.at() + 1; }
-
@Stoyan that give me -1 again
-
@Maarouf-Med-Mahdi
You should just change directory separator in your code from "\" to "/".
I make some tests with this code:QSqlDatabase mydb = QSqlDatabase::addDatabase("QSQLITE"); mydb.setDatabaseName("C:\Projects\Test\BD.db"); if(!mydb.open()) { qDebug() << mydb.lastError().text(); } qDebug() << "Tables: " << mydb.tables(); quint16 cnt = 0; QSqlQuery q; QString text_req = "select * from sqlite_master where tbl_name='serveurs'"; if(q.exec(text_req)) { while(q.next()) { cnt++; } qDebug() << "Records count: " << cnt; } else qDebug()<<"Invalide query"<<" --> " << q.lastError().text();
On compile it give warnings:
warning: unknown escape sequence: '\P' warning: unknown escape sequence: '\T' warning: unknown escape sequence: '\B'
The result was this (just like yours):
Tables: () Records count: 0
When I change this row:
mydb.setDatabaseName("C:\Projects\Test\BD.db");
with this:
mydb.setDatabaseName("C:/Projects/Test/BD.db");
there are no warnings, everything works and the result is:
Tables: ("serveurs", "historiques") Records count: 2
-
Just I want to give my experience result or a advice about this and to don't repeate to other members.
Sometimes, Qt does not accept an absolute path of the database file if it is not in the
same output program file (Debug or Release).
If Qt can not recover your database file, it will create a new empty database in the same output program folder. this can make the not such table sql error .
I want to thank all the members they help me for finding my proposed solution. -
Hi,
That's unrelated to Qt. It's how SQLite behaves. If there's no file matching the parameter you give it, it will create it. What you can do is check the file existence with QFile.
-
Hi @SGaist
Thanks for that SQLite information . I checked that .
all what i did to find that solution, i have moved the DataBase file to the output folder .