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. The SQLite exception "no such table: <table> Unable to execute statement" with QSqlDatabase
Qt 6.11 is out! See what's new in the release blog

The SQLite exception "no such table: <table> Unable to execute statement" with QSqlDatabase

Scheduled Pinned Locked Moved Solved General and Desktop
39 Posts 7 Posters 20.4k Views 2 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.
  • Maarouf Med MahdiM Offline
    Maarouf Med MahdiM Offline
    Maarouf Med Mahdi
    wrote on last edited by
    #30

    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

    1 Reply Last reply
    0
    • Maarouf Med MahdiM Offline
      Maarouf Med MahdiM Offline
      Maarouf Med Mahdi
      wrote on last edited by
      #31

      @Stoyan that query
      select * from sqlite_master where tbl_name = 'serveurs';
      works in the both

      S 1 Reply Last reply
      0
      • Maarouf Med MahdiM Maarouf Med Mahdi

        @Stoyan that query
        select * from sqlite_master where tbl_name = 'serveurs';
        works in the both

        S Offline
        S Offline
        Stoyan
        wrote on last edited by
        #32

        @Maarouf-Med-Mahdi
        It always works. The question is how many rows returns in each case.

        Maarouf Med MahdiM 1 Reply Last reply
        0
        • S Stoyan

          @Maarouf-Med-Mahdi
          It always works. The question is how many rows returns in each case.

          Maarouf Med MahdiM Offline
          Maarouf Med MahdiM Offline
          Maarouf Med Mahdi
          wrote on last edited by Maarouf Med Mahdi
          #33

          @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

          S 1 Reply Last reply
          0
          • Maarouf Med MahdiM Maarouf Med Mahdi

            @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

            S Offline
            S Offline
            Stoyan
            wrote on last edited by
            #34

            @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;
            }
            Maarouf Med MahdiM 1 Reply Last reply
            1
            • S Stoyan

              @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;
              }
              Maarouf Med MahdiM Offline
              Maarouf Med MahdiM Offline
              Maarouf Med Mahdi
              wrote on last edited by
              #35

              @Stoyan that give me -1 again

              S 1 Reply Last reply
              0
              • Maarouf Med MahdiM Maarouf Med Mahdi

                @Stoyan that give me -1 again

                S Offline
                S Offline
                Stoyan
                wrote on last edited by Stoyan
                #36

                @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
                
                1 Reply Last reply
                0
                • Maarouf Med MahdiM Offline
                  Maarouf Med MahdiM Offline
                  Maarouf Med Mahdi
                  wrote on last edited by Maarouf Med Mahdi
                  #37

                  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.

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #38

                    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.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    1
                    • Maarouf Med MahdiM Offline
                      Maarouf Med MahdiM Offline
                      Maarouf Med Mahdi
                      wrote on last edited by Maarouf Med Mahdi
                      #39

                      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 .

                      1 Reply Last reply
                      0

                      • Login

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