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. Strange behaviour of QSqlQuery

Strange behaviour of QSqlQuery

Scheduled Pinned Locked Moved General and Desktop
11 Posts 2 Posters 2.2k 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
    Basch006
    wrote on last edited by
    #1

    Hi everybody !

    I have a problem with one of my queries, but not the others.
    The query works when I write it "directly" on my server manager, but not via my Qt program.

    Here, in this code, the two last queries (insertPeseeDouble and updatePeseeOut) work when I call them in the program. But the first one (insertPeseeIn), when I call it in my program, fails. But works on the server manager.

    All the three queries are used on the same page, by the same database and written on the same cpp file.

    Here is the error I get :
    @QSqlQuery::exec: database not open
    "Driver not loaded Driver not loaded@

    @int AccesBD::insertPeseeIn(int idFkBalanceIn, int idFkTransport, int idFkProduit, float quantite, int IdFkUniteMesure, QString etat, bool utilisation)
    {
    QSqlQuery query;
    if(query.exec(QString("INSERT INTO [dbo].[Pesee] (Datetime_in, Id_fk_balance_in, Id_fk_transport, Id_fk_produit, Quantite_in, Id_fk_unite_mesure, Etat, Utilisation) VALUES(CURRENT_TIMESTAMP, %1,%2,%3,%4,%5,'%6','%7');").arg(idFkBalanceIn).arg(idFkTransport).arg(idFkProduit).arg(quantite).arg(IdFkUniteMesure).arg(etat).arg(utilisation)))
    {
    QSqlQuery queryLast("SELECT IDENT_CURRENT('[dbo].[Pesee]');");
    queryLast.next();
    return queryLast.value(0).toInt();
    }
    return -1;
    }

    int AccesBD::insertPeseeDouble(int idFkBalanceIn, int idFkBalanceOut, int idFkTransport, int idFkProduit, float quantiteIn, float quantiteOut, int IdFkUniteMesure, QString etat, bool utilisation)
    {
    QSqlQuery query;
    if(query.exec(QString("INSERT INTO [dbo].[Pesee] (Datetime_in, Datetime_out, Id_fk_balance_in, Id_fk_balance_out, Id_fk_transport, Id_fk_produit, Quantite_in, Quantite_out, Id_fk_unite_mesure, Etat, Utilisation) VALUES(CURRENT_TIMESTAMP, CURRENT_TIMESTAMP,%1,%2,%3,%4,%5,%6,%7,'%8','%9');").arg(idFkBalanceIn).arg(idFkBalanceOut).arg(idFkTransport).arg(idFkProduit).arg(quantiteIn).arg(quantiteOut).arg(IdFkUniteMesure).arg(etat).arg(utilisation)))
    {
    QSqlQuery queryLast("SELECT IDENT_CURRENT('[dbo].[Pesee]');");
    queryLast.next();
    return queryLast.value(0).toInt();
    }
    return -1;
    }

    bool AccesBD::updatePeseeOut(int idPesee,int idFkBalanceOut, float quantiteOut)
    {
    QSqlQuery query;
    return query.exec(QString("UPDATE [dbo].[Pesee] SET Datetime_out = CURRENT_TIMESTAMP, Id_fk_balance_out = %1, Quantite_out = %2 WHERE Id_Pesee = %3").arg(idFkBalanceOut).arg(quantiteOut).arg(idPesee));
    }@

    I really dont understand why :(

    If someone get an idea about that, it would be great :D
    Thanks anyway for the attentions.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi, what is your query.lastError?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        Basch006
        wrote on last edited by
        #3

        It is :

        "Driver not loaded Driver not loaded"

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          As the other queries work, this messages seems to be misleading.
          I suggest to look at the QString for your query by using qDebug().
          Probably this may be useful for you:
          @
          QString s = "INSERT INTO [dbo].[Pesee] ...";
          query.prepare(s);
          if (!query.exec()) {
          QMessageBox::warning(this, "ERR", "Error in query!\n" + query.lastError().text(), QMessageBox::Ok);
          qDebug() << s;

          @

          1 Reply Last reply
          0
          • B Offline
            B Offline
            Basch006
            wrote on last edited by
            #5

            That's not that finally -_-

            I test the call of this query in a basic program and it work.

            Actually, I just saw that in my original program, two of the arguments are null (= 0) and can't work because of the relation with other table I guess...
            I'll fix it before anything else and see what happen after that.

            Thanks anyway ^^

            1 Reply Last reply
            0
            • C Offline
              C Offline
              clochydd
              wrote on last edited by
              #6

              You're welcome.
              Have you considered to work with QSqlQuery::bindValue? This method is more transparent and easier to debug.

              1 Reply Last reply
              0
              • B Offline
                B Offline
                Basch006
                wrote on last edited by
                #7

                I tried to use this at the beginning, but I got some problems. I followed some tutorial about that but nothing worked so I changed my queries in that way :/

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  clochydd
                  wrote on last edited by
                  #8

                  I use it like this:
                  @
                  query = QSqlQuery(db);
                  QString s = "UPDATE mytable SET\n"
                  "field1 = :p01, field2 = :p02\n"
                  "WHERE fieldidx = :pID;";
                  query.prepare(s);
                  // WHERE clause:
                  query.bindValue(":pID", ui->lineEditID->text());
                  // SET fields:
                  query.bindValue(":p01", ui->lineEdit01->text());
                  query.bindValue(":p02", ui->lineEdit02->text());

                  if (!query.exec()) {
                  // show lastError:
                  qDebug() << query.lastError();
                  // show QString of query:
                  qDebug() << s;
                  }
                  @

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    Basch006
                    wrote on last edited by
                    #9

                    Is there a difference of performance/speed between the two methods ?

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      clochydd
                      wrote on last edited by
                      #10

                      I did not test it up to now.
                      But I am working with very large sql queries and I am absolutely satisfied with the performance (with both methods!).

                      1 Reply Last reply
                      0
                      • B Offline
                        B Offline
                        Basch006
                        wrote on last edited by
                        #11

                        I'll try again the bindValue so ^^

                        thx for your replies ;-)

                        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