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. QSql database - unable to update data
Qt 6.11 is out! See what's new in the release blog

QSql database - unable to update data

Scheduled Pinned Locked Moved Unsolved General and Desktop
15 Posts 5 Posters 5.0k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Hi,

    You are calling conn.connOpen twice, why ?

    On a side note, as already written in numerous threads these past few months: don't store local QSqlDatabase objects. The reasons are explained in the QSqlDatabase details.

    As a follow up question, why are re-opening that database again and again ?

    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
    2
    • VRoninV VRonin
      • What does qDebug()<<qry.lastError().text(); print?
      • NewEquipment conn; why are you creating this object?
      • prevent SQL Injections using QSqlQuery::addBindValue
      M Offline
      M Offline
      MR_mn
      wrote on last edited by
      #6

      @VRonin
      This is what I get from the qDebug:
      Connected to database
      QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
      QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
      Connected to database
      "No query Unable to fetch row"

      I'm creating that object to be able to be able to execute the connOpen and connClose functions.

      I will try to use the addBindValue and see if I can solve.

      I will come back later with the result.

      Thanks
      M.

      VRoninV 1 Reply Last reply
      0
      • M MR_mn

        @VRonin
        This is what I get from the qDebug:
        Connected to database
        QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
        QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
        Connected to database
        "No query Unable to fetch row"

        I'm creating that object to be able to be able to execute the connOpen and connClose functions.

        I will try to use the addBindValue and see if I can solve.

        I will come back later with the result.

        Thanks
        M.

        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #7

        @MR_mn said in QSql database - unable to update data:

        I'm creating that object to be able to be able to execute the connOpen and connClose functions.

        You don't need it, you can just call this->connOpen()

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • M Offline
          M Offline
          MR_mn
          wrote on last edited by
          #8

          I tried to play a little bit with my code following your suggestions, now qDebug output is:
          Connected to database
          "near "/": syntax error Unable to execute statement"
          " Parameter count mismatch"

          Here is new code:

          //#include "newequipment.h"
          #include "ui_newequipment.h"
          #include "secdialog.h"
          #include <QSql>
          #include <QSqlDatabase>
          #include <QSqlQuery>
          #include <QMessageBox>
          #include <QSqlError>
          
          NewEquipment::NewEquipment(QWidget *parent) :
              QDialog(parent),
              ui(new Ui::NewEquipment)
          {
              ui->setupUi(this);
          }
          
          NewEquipment::~NewEquipment()
          {
              delete ui;
          }
          
          void NewEquipment::on_pushButton_Save_clicked()
          {
          
          
              QString description, model, serial_number, manufacturer,
                      year_of_manufacturing, current_eng_hours, sling_SN;
              description=ui->lineEdit_Description->text();
              model=ui->lineEdit_Model->text();
              serial_number=ui->lineEdit_SN->text();
              manufacturer=ui->lineEdit_Manufacturer->text();
              year_of_manufacturing=ui->lineEdit_Yearofmanuf->text();
              current_eng_hours=ui->lineEdit_Enghours->text();
              sling_SN=ui->lineEdit_SlingSN->text();
          
          
          
              QSqlQuery qry;
              qry.prepare("INSERT INTO unita (Description,Model,S/N,Manufacturer,Year,Currentenginehours,SlingS/N) values (?, ?, ?, ?, ?, ?, ?)");
          
              qry.addBindValue(description);
              qry.addBindValue(model);
              qry.addBindValue(serial_number);
              qry.addBindValue(manufacturer);
              qry.addBindValue(year_of_manufacturing);
              qry.addBindValue(current_eng_hours);
              qry.addBindValue(sling_SN);
          
              qDebug()<<qry.lastError().text();
          
              qry.exec();
          
              if (qry.exec())
                  {
                  QMessageBox::information(this, "Saved!", "Saved data");
                  this->connClose();
                  }
                           else {
                               QMessageBox::critical(this, tr("Error"),"Data not saved");
                               qDebug()<<qry.lastError().text();
          
                                }
          
          }
          
          

          Here is the header file.

          #ifndef NEWEQUIPMENT_H
          #define NEWEQUIPMENT_H
          
          #include <QDialog>
          #include <QSqlDatabase>
          #include <QDebug>
          
          namespace Ui {
          class NewEquipment;
          }
          
          class NewEquipment : public QDialog
          {
              Q_OBJECT
          
          private:
          
              QSqlDatabase mydb;
          
              void connOpen()
              {
              mydb.addDatabase("QSQLITE");
              mydb.setDatabaseName("C:/ProgettiQT/GestioneManutenzione/Database_unita.db");
          
              if (!mydb.open()) { qDebug() << "Failed to connect to database"; }
              else { qDebug() << "Connected to database"; }
          
              }
          
              void connClose()
              {
              mydb.close();
              mydb.removeDatabase(QSqlDatabase::defaultConnection);
              }
          
          
          
          public:
              explicit NewEquipment(QWidget *parent = 0);
              ~NewEquipment();
          
          private slots:
              void on_pushButton_Save_clicked();
          
          private:
              Ui::NewEquipment *ui;
          };
          
          #endif // NEWEQUIPMENT_H
          
          

          Thanks for any advice!
          M.

          aha_1980A 1 Reply Last reply
          0
          • M MR_mn

            I tried to play a little bit with my code following your suggestions, now qDebug output is:
            Connected to database
            "near "/": syntax error Unable to execute statement"
            " Parameter count mismatch"

            Here is new code:

            //#include "newequipment.h"
            #include "ui_newequipment.h"
            #include "secdialog.h"
            #include <QSql>
            #include <QSqlDatabase>
            #include <QSqlQuery>
            #include <QMessageBox>
            #include <QSqlError>
            
            NewEquipment::NewEquipment(QWidget *parent) :
                QDialog(parent),
                ui(new Ui::NewEquipment)
            {
                ui->setupUi(this);
            }
            
            NewEquipment::~NewEquipment()
            {
                delete ui;
            }
            
            void NewEquipment::on_pushButton_Save_clicked()
            {
            
            
                QString description, model, serial_number, manufacturer,
                        year_of_manufacturing, current_eng_hours, sling_SN;
                description=ui->lineEdit_Description->text();
                model=ui->lineEdit_Model->text();
                serial_number=ui->lineEdit_SN->text();
                manufacturer=ui->lineEdit_Manufacturer->text();
                year_of_manufacturing=ui->lineEdit_Yearofmanuf->text();
                current_eng_hours=ui->lineEdit_Enghours->text();
                sling_SN=ui->lineEdit_SlingSN->text();
            
            
            
                QSqlQuery qry;
                qry.prepare("INSERT INTO unita (Description,Model,S/N,Manufacturer,Year,Currentenginehours,SlingS/N) values (?, ?, ?, ?, ?, ?, ?)");
            
                qry.addBindValue(description);
                qry.addBindValue(model);
                qry.addBindValue(serial_number);
                qry.addBindValue(manufacturer);
                qry.addBindValue(year_of_manufacturing);
                qry.addBindValue(current_eng_hours);
                qry.addBindValue(sling_SN);
            
                qDebug()<<qry.lastError().text();
            
                qry.exec();
            
                if (qry.exec())
                    {
                    QMessageBox::information(this, "Saved!", "Saved data");
                    this->connClose();
                    }
                             else {
                                 QMessageBox::critical(this, tr("Error"),"Data not saved");
                                 qDebug()<<qry.lastError().text();
            
                                  }
            
            }
            
            

            Here is the header file.

            #ifndef NEWEQUIPMENT_H
            #define NEWEQUIPMENT_H
            
            #include <QDialog>
            #include <QSqlDatabase>
            #include <QDebug>
            
            namespace Ui {
            class NewEquipment;
            }
            
            class NewEquipment : public QDialog
            {
                Q_OBJECT
            
            private:
            
                QSqlDatabase mydb;
            
                void connOpen()
                {
                mydb.addDatabase("QSQLITE");
                mydb.setDatabaseName("C:/ProgettiQT/GestioneManutenzione/Database_unita.db");
            
                if (!mydb.open()) { qDebug() << "Failed to connect to database"; }
                else { qDebug() << "Connected to database"; }
            
                }
            
                void connClose()
                {
                mydb.close();
                mydb.removeDatabase(QSqlDatabase::defaultConnection);
                }
            
            
            
            public:
                explicit NewEquipment(QWidget *parent = 0);
                ~NewEquipment();
            
            private slots:
                void on_pushButton_Save_clicked();
            
            private:
                Ui::NewEquipment *ui;
            };
            
            #endif // NEWEQUIPMENT_H
            
            

            Thanks for any advice!
            M.

            aha_1980A Offline
            aha_1980A Offline
            aha_1980
            Lifetime Qt Champion
            wrote on last edited by aha_1980
            #9

            Hi @MR_mn :

            qry.prepare("INSERT INTO unita (Description,Model,S/N,Manufacturer,Year,Currentenginehours,SlingS/N) values (?, ?, ?, ?, ?, ?, ?)");

            Your database columns "S/N" and "SlingS/N" contains slashes (/) and I guess that's driving the SQL engine crazy.

            Take care with spaces and special chars. Most often, ASCII chars (a-z, A-Z 0-9) is allowed, often also the underscore (_), sometimes also the minus sign (-). All other characters can be dangerous when using text based "command" or programming languages.

            I started my computer experience with MS-DOS, where file names were limited to 8 characters and most special chars were forbidden; so I'm used to such limitations.

            Qt has to stay free or it will die.

            M 1 Reply Last reply
            3
            • aha_1980A aha_1980

              Hi @MR_mn :

              qry.prepare("INSERT INTO unita (Description,Model,S/N,Manufacturer,Year,Currentenginehours,SlingS/N) values (?, ?, ?, ?, ?, ?, ?)");

              Your database columns "S/N" and "SlingS/N" contains slashes (/) and I guess that's driving the SQL engine crazy.

              Take care with spaces and special chars. Most often, ASCII chars (a-z, A-Z 0-9) is allowed, often also the underscore (_), sometimes also the minus sign (-). All other characters can be dangerous when using text based "command" or programming languages.

              I started my computer experience with MS-DOS, where file names were limited to 8 characters and most special chars were forbidden; so I'm used to such limitations.

              M Offline
              M Offline
              MR_mn
              wrote on last edited by
              #10

              Hi @aha_1980 , thanks for your advice, I changed something into the database and now I managed to have the program work, I guess the problem was with my database.

              Now the strange thing is that whenever I insert new data, instead of having just one new row, I have two, and the data inside are the same. Any hint on this?

              Thanks
              M.

              JonBJ 1 Reply Last reply
              0
              • M MR_mn

                Hi @aha_1980 , thanks for your advice, I changed something into the database and now I managed to have the program work, I guess the problem was with my database.

                Now the strange thing is that whenever I insert new data, instead of having just one new row, I have two, and the data inside are the same. Any hint on this?

                Thanks
                M.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by
                #11

                @MR_mn
                That would be if you execute the INSERT twice, wouldn't it. And that's because you have 2 qry.exec() statements....

                M 1 Reply Last reply
                1
                • JonBJ JonB

                  @MR_mn
                  That would be if you execute the INSERT twice, wouldn't it. And that's because you have 2 qry.exec() statements....

                  M Offline
                  M Offline
                  MR_mn
                  wrote on last edited by
                  #12

                  @JonB I thought my problem was related to what you pointed out, but actually I don't see the second query.exec() statement, where is it? I tried to take off the bindValue part, but I'm still getting the same problem.

                  Thanks
                  M.

                  JonBJ 1 Reply Last reply
                  0
                  • M MR_mn

                    @JonB I thought my problem was related to what you pointed out, but actually I don't see the second query.exec() statement, where is it? I tried to take off the bindValue part, but I'm still getting the same problem.

                    Thanks
                    M.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #13

                    @MR_mn
                    Assuming your code is as you have posted above --- if it isn't, all bets are off, till you post the actual code you're currently using --- search it for the string qry.exec if you want to know where you have written qry.exec....

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

                      Hi,

                      From your code:

                      qDebug()<<qry.lastError().text();
                      
                          qry.exec(); << First call
                      
                          if (qry.exec()) << Second call
                      

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

                      JonBJ 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        Hi,

                        From your code:

                        qDebug()<<qry.lastError().text();
                        
                            qry.exec(); << First call
                        
                            if (qry.exec()) << Second call
                        
                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by
                        #15

                        @SGaist
                        You are too soft! ;-)

                        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