Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qsqlite: no such table <table> Unable to execute statement
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SamuelV
    wrote on last edited by
    #1

    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 :)

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by mrjj
      #2

      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 ?

      S 1 Reply Last reply
      2
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        projetbdd_db

        I would guess the filename is wrong.

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        1 Reply Last reply
        3
        • mrjjM mrjj

          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 ?

          S Offline
          S Offline
          SamuelV
          wrote on last edited by SamuelV
          #4

          @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 :)

          mrjjM Christian EhrlicherC 2 Replies Last reply
          0
          • S SamuelV

            @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 :)

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @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.

            1 Reply Last reply
            0
            • S SamuelV

              @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 :)

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @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.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved