Qsqlite: no such table <table> Unable to execute statement

  • Hello,

    I looked during a day for an answer to my problem during one day, but no answer for the moment.
    I try to connect my SQLite db to Qt, it works, but when i try the query : 'SELECT * FROM user',
    i get the error : 'no such table: user Unable to execute statement'. But my database have this table.

    According to what i've read, it's supposed to be problem of path, but after lot of tests i'm sure it's the good one, in the same folder of the debug output. Here is my code:

        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                qDebug() << db.lastError().text();
            } else{
                qDebug() << "Connected";
                qDebug() << "Tables: " << db.tables();
                QSqlQuery request(db);
                if (request.exec("SELECT * FROM user")){
                    qDebug() << "Request accepted : " << request.at() + 1;
                } else {
                    qDebug() << "Request rejected : " << request.lastError();

    And the output :

    Tables:  ()
    Request rejected : no such table: user Unable to execute statement

    Really hope you'll be able to help me.
    Thank's in advance :)

  • Lifetime Qt Champion

    Hi and welcome to the forums
    Make really sure it does in fact find the DB file as else it will create a new empty one.
    qDebug() << "Tables: " << db.tables();
    with out put
    Tables: ()

    then it seems like its empty db.

    So if you go to
    and open it with a tool like

    Does it in fact have the table user ?

  • Lifetime Qt Champion

    @SamuelV said in Qsqlite: no such table <table> Unable to execute statement:


    I would guess the filename is wrong.

  • @mrjj actually when i try db.tables(); the output is indeed (), but on SQLiteBrowser I have the tables.
    @Christian-Ehrlicher it's not, i made lot of tests, i also deleted the database which was named "projetbdd" and tried with this new DB. But same problem appear, i tried also with '.db' but nothing change

    EDIT: Okay I deleted the database again and tried a new one and it finally worked, I did it like 3 times, so i'm not sure where was the problem, anyway thanks for your help :)

  • Lifetime Qt Champion


    Not sure what happen either but i been fooled a few times tht it creates new DB and opens that if it cannot find the db from the path

    Anyway, good it works.

    A note.
    Instead of using a full direct path to the DB which can break very easy.

    you can use
    QCoreApplication::applicationDirPath() + "/databasename.db"

    applicationDirPath returns the path where app.exe is. When in Creator, its in the build folder and
    this way its easy to open it regardless of actual folder name
    DB just has to be near the .exe.

  • Lifetime Qt Champion

    @SamuelV I still think the name was wrong since window may hide the correct postfix. Simply trying to open it with QFile() before opening the database will show you if the filename is correct.

Log in to reply