Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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");
        db.setDatabaseName("C:/Users/33633/Documents/Projets/build-ProjetBDD-Desktop_Qt_5_15_0_MinGW_64_bit-Debug/projetbdd_db");
    
        if(!db.open())
            {
                qDebug() << db.lastError().text();
            } else{
    
                qDebug() << "Connected";
    
                qDebug() << "Tables: " << db.tables();
    
                QSqlQuery request(db);
    
                if (request.exec("SELECT * FROM user")){
                    request.last();
                    qDebug() << "Request accepted : " << request.at() + 1;
                } else {
                    qDebug() << "Request rejected : " << request.lastError();
                }
    
                db.close();
            }
    

    And the output :

    Connected 
    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.
    Seeing
    qDebug() << "Tables: " << db.tables();
    with out put
    Tables: ()

    then it seems like its empty db.

    So if you go to
    build-ProjetBDD-Desktop_Qt_5_15_0_MinGW_64_bit-Debug/projetbdd_db
    and open it with a tool like
    https://sqlitebrowser.org/

    Does it in fact have the table user ?


  • Qt Champions 2019

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

    projetbdd_db

    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

    @SamuelV

    Hi
    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
    given.

    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.


  • Qt Champions 2019

    @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