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
Forum Updated to NodeBB v4.3 + New Features

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 17.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 Maarouf Med Mahdi
    #17

    Hi @Stoyan that show me (), i think you saw my db i posted it ,
    all tables has been created but Qt can not find it

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

      @Stoyan previously I used the encoding "system", now I've changed to UTF-8 but the same error still see

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

        Hi @Stoyan that show me (), i think you saw my db i posted it ,
        all tables has been created but Qt can not find it

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

        @Maarouf-Med-Mahdi
        It seems that you are not connected to the database.
        Try to change your code for open database.
        Instead:

        if(mydb.open())
                qDebug() << "connected";
            else
                qDebug() << "deconnected";
        

        use this:

            if(!mydb.open())
            {
                qDebug() << mydb.lastError().text();
            }
        

        BTW, what is the result from query that you use to test database connection:

        select * from sqlite_master where tbl_name = 'serveurs';
        
        1 Reply Last reply
        0
        • Maarouf Med MahdiM Offline
          Maarouf Med MahdiM Offline
          Maarouf Med Mahdi
          wrote on last edited by Maarouf Med Mahdi
          #20

          @Stoyan connected and that query works
          select * from sqlite_master where tbl_name = 'serveurs';

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

            @Stoyan connected and that query works
            select * from sqlite_master where tbl_name = 'serveurs';

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

            @Maarouf-Med-Mahdi
            OK, but what is the result? Empty set or 2-3 rows?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              Stoyan
              wrote on last edited by
              #22

              In SQLite a database is a single file. And I think that, if database file don't exists it is created and practically the connection is made to new empty database.
              You can search on your computer for file with the name "GestionDataCenter_BD.db". I guess that there will be another such file in other folder than original database.

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

                @Stoyan i understood what you said, but i m sure i connected to the real database from a real path
                mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");0_1513280011928_QSL2.PNG

                S 1 Reply Last reply
                0
                • K Offline
                  K Offline
                  karlheinzreichel
                  wrote on last edited by
                  #24

                  I think the answer is quite simple:
                  use double backslash!

                  instead of

                   mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");
                  
                  use
                  
                   mydb.setDatabaseName("D:\\Applications\\App Gestion Data CenterV2\\GestionDataCenterV2\\GestionDataCenter_BD.db");
                  

                  regards
                  Karl-Heinz

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

                    Hi @karlheinzreichel , i got that message
                    connected
                    invalide requette --> "file is encrypted or is not a database Unable to execute statement"

                    1 Reply Last reply
                    0
                    • K Offline
                      K Offline
                      karlheinzreichel
                      wrote on last edited by
                      #26

                      @Maarouf-Med-Mahdi said in How insert data into sqlite database with Qt:

                      (no such table: <table> Unable to execute statement).

                      But before you get the error:
                      (no such table: <table> Unable to execute statement).

                      now you get
                      file is encrypted or is not a database Unable to execute statement

                      it's somehow different.

                      Have you tried to test the database with sqlite3 database application?

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

                        @karlheinzreichel
                        all queries it works well with SQLiteStudio and has the same database

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

                          @Stoyan i understood what you said, but i m sure i connected to the real database from a real path
                          mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");0_1513280011928_QSL2.PNG

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

                          @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.
                          
                          1 Reply Last reply
                          2
                          • Maarouf Med MahdiM Maarouf Med Mahdi

                            @karlheinzreichel
                            all queries it works well with SQLiteStudio and has the same database

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

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

                            1 Reply Last reply
                            0
                            • 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

                                          • Login

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