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. QSqlQuery Update doesn't work

QSqlQuery Update doesn't work

Scheduled Pinned Locked Moved Solved General and Desktop
14 Posts 4 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #2

    Hi,

    Please add error checking to your code and print the error.

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

    G 1 Reply Last reply
    0
    • SGaistS SGaist

      Hi,

      Please add error checking to your code and print the error.

      G Offline
      G Offline
      gabor53
      wrote on last edited by
      #3

      Hi @SGaist
      I added

      qDebug()  << "Updating date error: " << fixquery.lastError ();
      

      after

      fixquery.exec ();
      

      It printed the following error message:
      Updating date error: QSqlError("", "", "").

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

        You should print the text of lastError.

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

        G 1 Reply Last reply
        0
        • SGaistS SGaist

          You should print the text of lastError.

          G Offline
          G Offline
          gabor53
          wrote on last edited by
          #5

          @SGaist
          This was the text it printed in output.

          G 1 Reply Last reply
          0
          • G gabor53

            @SGaist
            This was the text it printed in output.

            G Offline
            G Offline
            gabor53
            wrote on last edited by
            #6

            @gabor53
            When I change

            fixquery.bindValue (":AdoptDate", modDate);
            

            to

            fixquery.bindValue (":AdoptDate", "11/25/2007");
            
            

            it works correctly and adds the date to the db. Looks like it doesn't like QString modDate I just can't figure why.

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

              Print the executed query, that will give you some additional clues.

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

              G 1 Reply Last reply
              0
              • SGaistS SGaist

                Print the executed query, that will give you some additional clues.

                G Offline
                G Offline
                gabor53
                wrote on last edited by
                #8

                @SGaist
                Please tell me how to print it. Thank you.

                mrjjM 1 Reply Last reply
                0
                • G gabor53

                  @SGaist
                  Please tell me how to print it. Thank you.

                  mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by
                  #9

                  @gabor53

                  I think this one can do it.
                  QString QSqlQuery::lastQuery() const

                  • Returns the text of the current query being used, or an empty string if there is no current query text.

                  and maybe
                  QSqlQuery QSqlQueryModel::query() const

                  so something like

                  if (model->query())
                  QString str=model->query()->lastQuery() ?

                  Disclaimer: Didn't check the actual output.

                  G 1 Reply Last reply
                  1
                  • mrjjM mrjj

                    @gabor53

                    I think this one can do it.
                    QString QSqlQuery::lastQuery() const

                    • Returns the text of the current query being used, or an empty string if there is no current query text.

                    and maybe
                    QSqlQuery QSqlQueryModel::query() const

                    so something like

                    if (model->query())
                    QString str=model->query()->lastQuery() ?

                    Disclaimer: Didn't check the actual output.

                    G Offline
                    G Offline
                    gabor53
                    wrote on last edited by
                    #10

                    @mrjj
                    I added the following after fixquery.exec():

                           qDebug()  << "Updating date error: " << fixquery.lastError ();
                                QString last;
                                last = fixquery.lastQuery ();
                                qDebug() << "Last query: " << last;
                                break;
                    

                    I've got the following output in Application Output:
                    QVariant: QVariant(QString, "11/26/2015")
                    modDate: "11/26/2015"
                    Updating date error: QSqlError("", "", "")
                    Last query: "UPDATE Items SET AdoptDate = :AdoptDate WHERE ID = :fixID"

                    This looks normal to me....

                    1 Reply Last reply
                    0
                    • Paul ColbyP Offline
                      Paul ColbyP Offline
                      Paul Colby
                      wrote on last edited by
                      #11

                      Hi @gabor53,

                      • What does fixquery.exec(); return? true or false?
                      • What does fixquery.numRowsAffected(); return?
                      • What type is fixID, how is it set, and what guarantee do you have that the database actually contains a row with that ID?

                      Cheers.

                      G 1 Reply Last reply
                      2
                      • Paul ColbyP Paul Colby

                        Hi @gabor53,

                        • What does fixquery.exec(); return? true or false?
                        • What does fixquery.numRowsAffected(); return?
                        • What type is fixID, how is it set, and what guarantee do you have that the database actually contains a row with that ID?

                        Cheers.

                        G Offline
                        G Offline
                        gabor53
                        wrote on last edited by
                        #12

                        Hi @Paul-Colby,

                        Last query: "UPDATE Items SET AdoptDate = :AdoptDate WHERE ID = :fixID"
                        Fixquery.exec() returns: Fixqueryexec is false
                        Rows affected: -1.

                        fixID is set like this:

                                    QDateEdit *dateEdit = qobject_cast<QDateEdit*>(editor);
                                    QModelIndex updateIndex(index.model ()->index(index.row (),0,index.parent ()));
                        
                                    QString fixID;
                                    QVariant v(updateIndex.data (Qt::DisplayRole));
                                    fixID = v.toString ();
                                    qDebug() << "FixID: " << fixID;
                        

                        FixID is a QString which will give the value to the ID column in the database.The ID column does exist in the db.

                        1 Reply Last reply
                        0
                        • G Offline
                          G Offline
                          gabor53
                          wrote on last edited by
                          #13

                          I finally figured out what the problem was. In the switch statement where this query is I made a typo:
                          instead of

                          case 7:
                          

                          I had

                          case 7:;
                          

                          After deleting the ;i t works fine.

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

                            Ok... So completely unrelated... Can't have a nicer bug :D

                            Glad you found out and thanks for sharing.

                            Since you have it working now, please mark the thread as solved using the "Topic Tools" button so that other forum users may know a solution has been found :)

                            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

                            • Login

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