Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. can't insert data in a sqlite3 database in qt 6
QtWS25 Last Chance

can't insert data in a sqlite3 database in qt 6

Scheduled Pinned Locked Moved Solved Qt 6
10 Posts 3 Posters 796 Views
  • 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.
  • B Offline
    B Offline
    burrito1111
    wrote on last edited by
    #1

    I have a test.db database on my project directory, which I'm trying to insert data into. The database is connected, but I can't seem to insert data in it. The query is not executed at all (it seems), since the qDebug shows "Bad".

    QSqlDatabase connectDB(){
        QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("test.db");
        return db;
    
    }
    
    void planner::on_dataSend_clicked()
    {
        QSqlDatabase datba = connectDB();
        if (datba.open()){
            qDebug()<< "DB Suc";
        } else{
            qDebug() << "DB Fail";
        }
        QString what = ui->addPlan->text();
        QSqlQuery qry;
        qry.prepare("insert into [plan] values :what");
        qry.bindValue(":what",what);
        if (qry.exec()){
                 qDebug()<< "Good";
                 qry.clear();
    
              } else{
                 qDebug()<<"Bad";
                 qDebug()<<qry.lastError();
                 qry.clear();
    
              }
        datba.close();
        ui->addPlan->clear();
    
    
    }
    

    QSqlError shows "parameter count mismatch" . I am using DB Browser for SQLite.

    1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      @burrito1111 said in can't insert data in a sqlite3 database in qt 6:

      and it worked.

      Because now the syntax is correct. But it will fail as soon as 'what' contains e.g. a ') or similar. Use a prepared statement and fix your syntax.

      B Offline
      B Offline
      burrito1111
      wrote on last edited by
      #9

      @Christian-Ehrlicher thanks for the heads up. I will try using the prepare statement correctly. will get back to you if i run into issues.

       q.prepare("insert into plan(plan) values (:plan)");
          q.bindValue(":plan",what);
          if (q.exec()){
              qDebug()<< "Ok";
          }
      

      works now.

      @JonB thanks a lot for the insight too.

      thanks a ton to both of you. hope you have a great day <3

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

        @burrito1111 said in can't insert data in a sqlite3 database in qt 6:

        qry.prepare("insert into [plan] values :what");

        Please check the return value of the prepare statement

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

        B 1 Reply Last reply
        1
        • B Offline
          B Offline
          burrito1111
          wrote on last edited by
          #3
          This post is deleted!
          1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @burrito1111 said in can't insert data in a sqlite3 database in qt 6:

            qry.prepare("insert into [plan] values :what");

            Please check the return value of the prepare statement

            B Offline
            B Offline
            burrito1111
            wrote on last edited by
            #4

            @Christian-Ehrlicher it returns false if I qDebug()<<query.prepare("INSERT INTO plan VALUES (:plan)");
            if that's what u meant.

            JonBJ 1 Reply Last reply
            0
            • B burrito1111

              @Christian-Ehrlicher it returns false if I qDebug()<<query.prepare("INSERT INTO plan VALUES (:plan)");
              if that's what u meant.

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

              @burrito1111
              That is what he meant, and it means SQLite does not accept it.
              Your syntax is why you get QSqlError shows "parameter count mismatch" . from ("insert into [plan] values :what)
              So fix your statement, before you can insert anything.

              B 1 Reply Last reply
              0
              • JonBJ JonB

                @burrito1111
                That is what he meant, and it means SQLite does not accept it.
                Your syntax is why you get QSqlError shows "parameter count mismatch" . from ("insert into [plan] values :what)
                So fix your statement, before you can insert anything.

                B Offline
                B Offline
                burrito1111
                wrote on last edited by
                #6

                @JonB hi jonb. yes i think it was the syntax too. I used the q.exec directly and it worked.

                QString what = ui->addPlan->text();
                    QSqlQuery q;
                    if(q.exec("insert into plan(plan) values ('"+what+"')")){
                        qDebug()<< "Ok";
                    };
                
                Christian EhrlicherC JonBJ 2 Replies Last reply
                0
                • B burrito1111

                  @JonB hi jonb. yes i think it was the syntax too. I used the q.exec directly and it worked.

                  QString what = ui->addPlan->text();
                      QSqlQuery q;
                      if(q.exec("insert into plan(plan) values ('"+what+"')")){
                          qDebug()<< "Ok";
                      };
                  
                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #7

                  @burrito1111 said in can't insert data in a sqlite3 database in qt 6:

                  and it worked.

                  Because now the syntax is correct. But it will fail as soon as 'what' contains e.g. a ') or similar. Use a prepared statement and fix your syntax.

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

                  B 1 Reply Last reply
                  2
                  • B burrito1111

                    @JonB hi jonb. yes i think it was the syntax too. I used the q.exec directly and it worked.

                    QString what = ui->addPlan->text();
                        QSqlQuery q;
                        if(q.exec("insert into plan(plan) values ('"+what+"')")){
                            qDebug()<< "Ok";
                        };
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #8

                    @burrito1111
                    That means/implies it does not accept a bound value (like :what) in the values segment if that is so. Which I don't think is the case. Try again now that you know what the correct syntax for insert is, which you had wrong earlier.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Christian Ehrlicher

                      @burrito1111 said in can't insert data in a sqlite3 database in qt 6:

                      and it worked.

                      Because now the syntax is correct. But it will fail as soon as 'what' contains e.g. a ') or similar. Use a prepared statement and fix your syntax.

                      B Offline
                      B Offline
                      burrito1111
                      wrote on last edited by
                      #9

                      @Christian-Ehrlicher thanks for the heads up. I will try using the prepare statement correctly. will get back to you if i run into issues.

                       q.prepare("insert into plan(plan) values (:plan)");
                          q.bindValue(":plan",what);
                          if (q.exec()){
                              qDebug()<< "Ok";
                          }
                      

                      works now.

                      @JonB thanks a lot for the insight too.

                      thanks a ton to both of you. hope you have a great day <3

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

                        But you still should check the return values of prepare() and exec() and print out the error string when there is an error.

                        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
                        1

                        • Login

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