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. SetQuery INSERT
QtWS25 Last Chance

SetQuery INSERT

Scheduled Pinned Locked Moved Solved General and Desktop
sqlite
13 Posts 5 Posters 4.9k 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.
  • G Offline
    G Offline
    gabor53
    wrote on 13 Dec 2015, 04:55 last edited by
    #1

    I'm using an sqlite database with the following INSERT:
    model->setQuery ("INSERT INTO People (ID,FirstName,LastName)""VALUES('4','John','Smith");
    I'd like to use variables as values such as ID for for, Fname for John, Lname for Smith.
    What should I change to make the query work?
    I tried
    setQuery ("INSERT INTO People (ID,FirstName,LastName)""VALUES(ID,Fname,Lname");
    but it doesn't work.
    Thank you for your help.

    K 1 Reply Last reply 13 Dec 2015, 05:41
    0
    • G gabor53
      13 Dec 2015, 04:55

      I'm using an sqlite database with the following INSERT:
      model->setQuery ("INSERT INTO People (ID,FirstName,LastName)""VALUES('4','John','Smith");
      I'd like to use variables as values such as ID for for, Fname for John, Lname for Smith.
      What should I change to make the query work?
      I tried
      setQuery ("INSERT INTO People (ID,FirstName,LastName)""VALUES(ID,Fname,Lname");
      but it doesn't work.
      Thank you for your help.

      K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 13 Dec 2015, 05:41 last edited by kshegunov
      #2

      @gabor53
      Hello,
      QSqlQueryModel (assuming that you're using that class) is for displaying data. I don't understand what the model is supposed to display if you give it an insert query ... Maybe elaborate a bit on what you're trying to achieve? For example, if you're interested in just inserting data into the SQL database, you could use the QSqlQuery class to do just that.
      As a side note, you seem to have some quotes doubled in the middle of the queries and the strings actually don't seem quite right, is this a typo?

      Kind regards.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mjsurette
        wrote on 13 Dec 2015, 17:49 last edited by
        #3

        @kshegunov

        As @gabor53 said, you're not looking for a model to insert data. What you need is QSqlQuery. In particular the prepare and execute methods.

        1 Reply Last reply
        0
        • G Offline
          G Offline
          gabor53
          wrote on 14 Dec 2015, 04:59 last edited by
          #4

          I tried to fix the problem like this:

          #include <QCoreApplication>
          #include <QDebug>
          #include <QtGui>
          #include <QtCore>
          #include <QtSql>
          #include <QtDebug>
          #include <QSqlQueryModel>
          
          int main(int argc, char *argv[])
          {
              QCoreApplication a(argc, argv);
          
              QSqlDatabase db;
              QSqlQuery query;
          
              db = QSqlDatabase::addDatabase ("QSQLITE");
              db.setDatabaseName ("C:/Programming/Qtsamples/Variabletodb/database.db");
              db.open ();
          
              if(!db.open ())
              {
                  qDebug() << "The database is not open!";
              }
          	else
              {
                  qDebug() << "The database is open!";
              }
          
              QString ID = "213";
              QString Fname = "John";
              QString Lname = "Smith";
          
              query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                             "VALUES(:ID, :FirstName, :LastName)");
              query.bindValue (0,ID);
              query.bindValue (1,Fname);
              query.bindValue (2,Lname);
              query.exec ();
              db.close ();
          
          
              return a.exec();
          }
          
          

          I got the following messages:
          The database is open!
          QSqlQuery::prepare: database not open

          Of course nothing was written into the database. Please tell me what I did wrong at this time. Thank you for your help.

          K J 2 Replies Last reply 14 Dec 2015, 05:13
          0
          • G gabor53
            14 Dec 2015, 04:59

            I tried to fix the problem like this:

            #include <QCoreApplication>
            #include <QDebug>
            #include <QtGui>
            #include <QtCore>
            #include <QtSql>
            #include <QtDebug>
            #include <QSqlQueryModel>
            
            int main(int argc, char *argv[])
            {
                QCoreApplication a(argc, argv);
            
                QSqlDatabase db;
                QSqlQuery query;
            
                db = QSqlDatabase::addDatabase ("QSQLITE");
                db.setDatabaseName ("C:/Programming/Qtsamples/Variabletodb/database.db");
                db.open ();
            
                if(!db.open ())
                {
                    qDebug() << "The database is not open!";
                }
            	else
                {
                    qDebug() << "The database is open!";
                }
            
                QString ID = "213";
                QString Fname = "John";
                QString Lname = "Smith";
            
                query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                               "VALUES(:ID, :FirstName, :LastName)");
                query.bindValue (0,ID);
                query.bindValue (1,Fname);
                query.bindValue (2,Lname);
                query.exec ();
                db.close ();
            
            
                return a.exec();
            }
            
            

            I got the following messages:
            The database is open!
            QSqlQuery::prepare: database not open

            Of course nothing was written into the database. Please tell me what I did wrong at this time. Thank you for your help.

            K Offline
            K Offline
            kshegunov
            Moderators
            wrote on 14 Dec 2015, 05:13 last edited by
            #5

            @gabor53
            Hello,
            You should use named bindings, like this:

            query.bindValue(":ID", ID);
            query.bindValue(":FirstName", Fname);
            // ... and so on
            

            Additionally, how does

            query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                               "VALUES(:ID, :FirstName, :LastName)");
            

            even compile? you have two strings on two lines, and the function expects a single string ...

            Read and abide by the Qt Code of Conduct

            J 1 Reply Last reply 14 Dec 2015, 05:22
            0
            • K kshegunov
              14 Dec 2015, 05:13

              @gabor53
              Hello,
              You should use named bindings, like this:

              query.bindValue(":ID", ID);
              query.bindValue(":FirstName", Fname);
              // ... and so on
              

              Additionally, how does

              query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                                 "VALUES(:ID, :FirstName, :LastName)");
              

              even compile? you have two strings on two lines, and the function expects a single string ...

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Dec 2015, 05:22 last edited by
              #6

              @kshegunov This is valid syntax: compiler combines both string literals into one string.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              K 1 Reply Last reply 14 Dec 2015, 05:28
              0
              • G gabor53
                14 Dec 2015, 04:59

                I tried to fix the problem like this:

                #include <QCoreApplication>
                #include <QDebug>
                #include <QtGui>
                #include <QtCore>
                #include <QtSql>
                #include <QtDebug>
                #include <QSqlQueryModel>
                
                int main(int argc, char *argv[])
                {
                    QCoreApplication a(argc, argv);
                
                    QSqlDatabase db;
                    QSqlQuery query;
                
                    db = QSqlDatabase::addDatabase ("QSQLITE");
                    db.setDatabaseName ("C:/Programming/Qtsamples/Variabletodb/database.db");
                    db.open ();
                
                    if(!db.open ())
                    {
                        qDebug() << "The database is not open!";
                    }
                	else
                    {
                        qDebug() << "The database is open!";
                    }
                
                    QString ID = "213";
                    QString Fname = "John";
                    QString Lname = "Smith";
                
                    query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                                   "VALUES(:ID, :FirstName, :LastName)");
                    query.bindValue (0,ID);
                    query.bindValue (1,Fname);
                    query.bindValue (2,Lname);
                    query.exec ();
                    db.close ();
                
                
                    return a.exec();
                }
                
                

                I got the following messages:
                The database is open!
                QSqlQuery::prepare: database not open

                Of course nothing was written into the database. Please tell me what I did wrong at this time. Thank you for your help.

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 14 Dec 2015, 05:24 last edited by
                #7

                @gabor53 I think you should pass the database to the query:

                QSqlQuery query(db);
                

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • J jsulm
                  14 Dec 2015, 05:22

                  @kshegunov This is valid syntax: compiler combines both string literals into one string.

                  K Offline
                  K Offline
                  kshegunov
                  Moderators
                  wrote on 14 Dec 2015, 05:28 last edited by
                  #8

                  @jsulm

                  This is valid syntax: compiler combines both string literals into one string.

                  It is possible, I have never seen it before though.

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    mjsurette
                    wrote on 14 Dec 2015, 14:10 last edited by
                    #9

                    @gabor53
                    @jsulm is right, you need to pass the database to the query. I always do this after the database is open. This assumes of course that the database is already created and the table defined.

                    @kshegunov C++ and I believe C as well concatenates two or more string literals separated only by whitespace. This is quite useful for maintaining readability in long SQL queries.

                    K 1 Reply Last reply 14 Dec 2015, 15:38
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 14 Dec 2015, 14:24 last edited by
                      #10

                      Hi,

                      @jsulm and @mjsurette Passing the database to QSqlQuery's constructor is only needed if you don't use the default connection which is not the case.

                      However, query is created before the database is added thus using an invalid connection . That's way there's a complaint about a closed database.

                      @kshegunov Indeed, the two strings are concatenated but here there's a space that will be missing in the current case.

                      What should also be done is check the return value of query.exec() and print the related error if it fails. Otherwise there's no way to have a real clue of what is going on.

                      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
                      0
                      • M mjsurette
                        14 Dec 2015, 14:10

                        @gabor53
                        @jsulm is right, you need to pass the database to the query. I always do this after the database is open. This assumes of course that the database is already created and the table defined.

                        @kshegunov C++ and I believe C as well concatenates two or more string literals separated only by whitespace. This is quite useful for maintaining readability in long SQL queries.

                        K Offline
                        K Offline
                        kshegunov
                        Moderators
                        wrote on 14 Dec 2015, 15:38 last edited by
                        #11

                        @mjsurette @SGaist
                        Thanks for the clearing that up. I never knew this to be possible and I always have used the \ + newline approach (the way macros are broken down to multiple lines).

                        Read and abide by the Qt Code of Conduct

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          gabor53
                          wrote on 14 Dec 2015, 21:18 last edited by
                          #12

                          Thanks for all your help.
                          I added

                           qDebug()<< query.lastError ();
                          

                          and I got the following message:
                          QSqlError("","Driver not loaded","Driver not loaded").

                           qDebug()<< query.lastError ();
                          

                          returns false.
                          Any idea what's wrong?
                          Thank you.

                          1 Reply Last reply
                          0
                          • G Offline
                            G Offline
                            gabor53
                            wrote on 14 Dec 2015, 21:37 last edited by
                            #13

                            The following code worked:

                            int main(int argc, char *argv[])
                            {
                                QCoreApplication a(argc, argv);
                            
                                QSqlDatabase db;
                            
                            
                                db = QSqlDatabase::addDatabase ("QSQLITE");
                                db.setDatabaseName ("C:/Programming/Qtsamples/Variabletodb/database.db");
                                db.open ();
                            	QSqlQuery query(db);
                            
                                if(!db.open ())
                                {
                                    qDebug() << "The database is not open!";
                                }
                            	else
                                {
                                    qDebug() << "The database is open!";
                                }
                            
                                QString ID = "213";
                                QString Fname = "John";
                                QString Lname = "Smith";
                            
                                query.prepare ("INSERT INTO People (ID, FirstName, LastName)"
                                               "VALUES(:ID, :FirstName, :LastName)");
                                query.bindValue (":ID",ID);
                                query.bindValue (":FirstName",Fname);
                                query.bindValue (":LastName",Lname);
                                query.exec ();
                            
                            
                            
                            //    qDebug() <<  query.exec();
                            
                            //    qDebug()<< query.lastError ();
                            
                                db.close ();
                            
                            
                                return a.exec();
                            }
                            
                            

                            Thank you for your help!

                            1 Reply Last reply
                            0

                            5/13

                            14 Dec 2015, 05:13

                            topic:navigator.unread, 8
                            • Login

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