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.3k 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
    #1

    welcome,
    I opened my database sqlite in Qt without error and I checked that all the tables are well created in the base and well the path of the base. but when I try to execute a query I get an error (no such table: <table> Unable to execute statement).
    I am sure that I connected to a real base and that it contains all the table .
    Someone you can help me please.

    Taz742T JonBJ 2 Replies Last reply
    0
    • Maarouf Med MahdiM Maarouf Med Mahdi

      welcome,
      I opened my database sqlite in Qt without error and I checked that all the tables are well created in the base and well the path of the base. but when I try to execute a query I get an error (no such table: <table> Unable to execute statement).
      I am sure that I connected to a real base and that it contains all the table .
      Someone you can help me please.

      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2

      @Maarouf-Med-Mahdi
      Can you post your query?

      Do what you want.

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

        @Taz742

        // --connecting data base code :
        
           mydb=QSqlDatabase::addDatabase("QSQLITE");
        
            mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");
            if(mydb.open())
                qDebug() << "connected";
            else
                qDebug() << "deconnected";
        
        // --executing query code : 
         QSqlQuery requette;
         QString text_req = "insert into serveurs values('123.112.113','Phisique',1,2,3,3,'01/01/1997','12/01/1997');";
        
         if(requette.exec(text_req))
             qDebug()<<"requette valide";
         else
             qDebug()<<"invalide requette"<<" --> "<<requette.lastError().text();
        
        Taz742T 1 Reply Last reply
        0
        • Maarouf Med MahdiM Maarouf Med Mahdi

          @Taz742

          // --connecting data base code :
          
             mydb=QSqlDatabase::addDatabase("QSQLITE");
          
              mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");
              if(mydb.open())
                  qDebug() << "connected";
              else
                  qDebug() << "deconnected";
          
          // --executing query code : 
           QSqlQuery requette;
           QString text_req = "insert into serveurs values('123.112.113','Phisique',1,2,3,3,'01/01/1997','12/01/1997');";
          
           if(requette.exec(text_req))
               qDebug()<<"requette valide";
           else
               qDebug()<<"invalide requette"<<" --> "<<requette.lastError().text();
          
          Taz742T Offline
          Taz742T Offline
          Taz742
          wrote on last edited by
          #4

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

          insert into serveurs values('123.112.113','Phisique',1,2,3,3,'01/01/1997','12/01/1997');"

          It is a valid query?
          Does this query work in sql without Qt?

          Do what you want.

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

            Yes , i tested the same query before execute with Qt .

            1 Reply Last reply
            0
            • Vinod KuntojiV Offline
              Vinod KuntojiV Offline
              Vinod Kuntoji
              wrote on last edited by
              #6

              @Maarouf-Med-Mahdi ,

              Path of DB file contains some spaces. Remove those spaces and try.

              C++, Qt, Qt Quick Developer,
              PthinkS, Bangalore

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

                @Vinod-Kuntoji
                qt connected well to my database and i have run other query like
                select * from sqlite_master where tbl_name = '<table>' "; it works to show you that the path of bd is valid,

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

                  welcome,
                  I opened my database sqlite in Qt without error and I checked that all the tables are well created in the base and well the path of the base. but when I try to execute a query I get an error (no such table: <table> Unable to execute statement).
                  I am sure that I connected to a real base and that it contains all the table .
                  Someone you can help me please.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @Maarouf-Med-Mahdi
                  You said:

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

                  So it's telling you there is no table named whatever it reports for <table> (really wish you'd give the actual text of the error message, then we'd know....)

                  Your statement is:
                  insert into serveurs ...
                  so, assuming it reports serveurs as the table name, there is no such table as serveurs in whatever is your current database....

                  P.S.
                  Have you tried SELECT * FROM serveurs in your Qt code for yourself, to decide whether it's really an INSERT problem?

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

                    @Vinod-Kuntoji
                    qt connected well to my database and i have run other query like
                    select * from sqlite_master where tbl_name = '<table>' "; it works to show you that the path of bd is valid,

                    Taz742T Offline
                    Taz742T Offline
                    Taz742
                    wrote on last edited by Taz742
                    #9

                    @Maarouf-Med-Mahdi
                    Try:

                    QSqlQuery query;
                    query.prepare("insert into serveurs (field1, field2, field3) values (:value1, :value2, :value3)");
                    query.bindValue(":value1," MyValue1);
                    query.bindValue(":value2", MyValue2);
                    query.bindValue(":value3", MyValue3);
                    
                    if (query.exec()) {
                        //
                    } else {
                        qDebug() << query.lastError().text();
                    }
                    

                    Do what you want.

                    JonBJ 1 Reply Last reply
                    1
                    • Taz742T Taz742

                      @Maarouf-Med-Mahdi
                      Try:

                      QSqlQuery query;
                      query.prepare("insert into serveurs (field1, field2, field3) values (:value1, :value2, :value3)");
                      query.bindValue(":value1," MyValue1);
                      query.bindValue(":value2", MyValue2);
                      query.bindValue(":value3", MyValue3);
                      
                      if (query.exec()) {
                          //
                      } else {
                          qDebug() << query.lastError().text();
                      }
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by JonB
                      #10

                      @Taz742
                      https://sqlite.org/lang_insert.html is telling you that the list of column/field names is optional in INSERT INTO, if that's what you're trying to tell the OP to correct to.

                      1 Reply Last reply
                      0
                      • Vinod KuntojiV Offline
                        Vinod KuntojiV Offline
                        Vinod Kuntoji
                        wrote on last edited by
                        #11

                        @Maarouf-Med-Mahdi ,
                        Are you sure, your table name is correct?

                        C++, Qt, Qt Quick Developer,
                        PthinkS, Bangalore

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

                          @Vinod-Kuntoji yes i m sure

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

                            @JNBarchan that is my dataBase file

                            -- Fichier généré par SQLiteStudio v3.1.1 sur mer. déc. 13 21:27:34 2017

                            -- Encodage texte utilisé : System

                            PRAGMA foreign_keys = off;
                            BEGIN TRANSACTION;

                            -- Table : historiques
                            CREATE TABLE historiques(
                            IP varchar(20) ,
                            date_modification date,
                            anc_type_serveur varchar(10),
                            anc_croire int (10),
                            anc_rack int(10),
                            anc_chassis int (4),
                            anc_position int (10),
                            constraint FK_historiques foreign key (IP) references serveurs(IP)
                            );

                            -- Table : serveurs
                            CREATE TABLE serveurs (
                            IP varchar(20) ,
                            type_serveur varchar(10),
                            croire int (10),
                            rack int(10),
                            chassis int (4),
                            position int (10),
                            date_creation date,
                            date_der_accee date,
                            constraint PK_serveurs primary key (IP)
                            );

                            COMMIT TRANSACTION;
                            PRAGMA foreign_keys = on;

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

                              @JNBarchan I give you the result of this query
                              select * from serveurs;
                              ***the code:
                              mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");
                              if(mydb.open())
                              qDebug() << "connected";
                              else
                              qDebug() << "deconnected";

                              QSqlQuery requette;
                              QString text_req = "select * from serveurs;";
                              if(requette.exec(text_req))
                              qDebug()<<"requette valide";
                              else
                              qDebug()<<"invalide requette"<<" --> "<<requette.lastError().text();
                              ***the debug
                              connected
                              invalide requette --> "no such table: serveurs Unable to execute statement"

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

                                @Taz742 this query shows me the same error
                                QString text_req = "insert into serveurs('IP','type_serveur','croire','rack','chassis','position','date_creation','date_der_accee') values('123.112.113','Physique',1,2,3,3,'01/01/1997','12/01/1997');";

                                // I wish you saw my database I posted it

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

                                  @JNBarchan I give you the result of this query
                                  select * from serveurs;
                                  ***the code:
                                  mydb.setDatabaseName("D:\Applications\App Gestion Data CenterV2\GestionDataCenterV2\GestionDataCenter_BD.db");
                                  if(mydb.open())
                                  qDebug() << "connected";
                                  else
                                  qDebug() << "deconnected";

                                  QSqlQuery requette;
                                  QString text_req = "select * from serveurs;";
                                  if(requette.exec(text_req))
                                  qDebug()<<"requette valide";
                                  else
                                  qDebug()<<"invalide requette"<<" --> "<<requette.lastError().text();
                                  ***the debug
                                  connected
                                  invalide requette --> "no such table: serveurs Unable to execute statement"

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

                                  @Maarouf-Med-Mahdi
                                  This message also prove that there is no such table in your database.
                                  You can check which tables are available with this code:

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

                                  Maybe there is a difference in some letter in other code page (UTF-8 vs ANSI).

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

                                          • Login

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