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. C++ sqlite commit db after an update
Forum Updated to NodeBB v4.3 + New Features

C++ sqlite commit db after an update

Scheduled Pinned Locked Moved Solved General and Desktop
24 Posts 5 Posters 9.2k Views 2 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.
  • G Offline
    G Offline
    gfxx
    wrote on 22 Aug 2016, 11:37 last edited by gfxx
    #1
    QSqlDatabase db;
    
        QSqlQuery query2(db);
        query2.prepare("SELECT MAX(id_stc) FROM stc ");
        query2.exec();
        query2.next();
        index2 = query2.value(0).toInt();
        query2.clear();
    
        QSqlQuery query21(db);
        query21.prepare("UPDATE stc SET id_st=:id_st, h_on=:h_on, h_off=:h_off, err_m=:err_m, t_em=:t_em, t_pause=:t_pause, p_pro=:p_pro WHERE id_st =:id_st");
        query21.bindValue(":id_st", index2 );
        query21.bindValue(":h_on", HOn);
        query21.bindValue(":h_off", HOff);
        query21.bindValue(":err_m", errM);
        query21.bindValue(":t_em", tEme);
        query21.bindValue(":t_pause", tPa);
        query21.bindValue(":p_pro", pPro);
        query21.exec();
        query21.next();
    /*db.commit(); ... if I put the commit command here is the same*/
        query21.clear();
        db.commit();
    

    my piece of code .... it works great but if I make an insert with other void (myINSERTvoid) I can't able to make an UPDATE on real last roid .... If stop my app and run again i can see last roid and make correctly the UPDATE .... obviusly if make an INSERT statement first and an UPDATE second the problem comes out again ....

    I'm on linux ubuntu 14.04 ... with sqlite3 ... I just install all dependancy and if I insert 1 row for control driver ...

    qDebug ( )  <<  QSqlDatabase::drivers();
    

    I obtain the correct response .....

    I have some problem but not see it...

    Thanks for any helps

    Regards
    Giorgio

    bkt

    T 1 Reply Last reply 22 Aug 2016, 11:47
    0
    • G gfxx
      22 Aug 2016, 11:37
      QSqlDatabase db;
      
          QSqlQuery query2(db);
          query2.prepare("SELECT MAX(id_stc) FROM stc ");
          query2.exec();
          query2.next();
          index2 = query2.value(0).toInt();
          query2.clear();
      
          QSqlQuery query21(db);
          query21.prepare("UPDATE stc SET id_st=:id_st, h_on=:h_on, h_off=:h_off, err_m=:err_m, t_em=:t_em, t_pause=:t_pause, p_pro=:p_pro WHERE id_st =:id_st");
          query21.bindValue(":id_st", index2 );
          query21.bindValue(":h_on", HOn);
          query21.bindValue(":h_off", HOff);
          query21.bindValue(":err_m", errM);
          query21.bindValue(":t_em", tEme);
          query21.bindValue(":t_pause", tPa);
          query21.bindValue(":p_pro", pPro);
          query21.exec();
          query21.next();
      /*db.commit(); ... if I put the commit command here is the same*/
          query21.clear();
          db.commit();
      

      my piece of code .... it works great but if I make an insert with other void (myINSERTvoid) I can't able to make an UPDATE on real last roid .... If stop my app and run again i can see last roid and make correctly the UPDATE .... obviusly if make an INSERT statement first and an UPDATE second the problem comes out again ....

      I'm on linux ubuntu 14.04 ... with sqlite3 ... I just install all dependancy and if I insert 1 row for control driver ...

      qDebug ( )  <<  QSqlDatabase::drivers();
      

      I obtain the correct response .....

      I have some problem but not see it...

      Thanks for any helps

      Regards
      Giorgio

      T Offline
      T Offline
      the_
      wrote on 22 Aug 2016, 11:47 last edited by the_
      #2

      @gfxx

      commit() ends a transaction, but I can't see where you start your transaction.

      //Edit
      What do you get with db.lastError(); ?

      -- No support in PM --

      1 Reply Last reply
      2
      • V Offline
        V Offline
        VRonin
        wrote on 22 Aug 2016, 12:51 last edited by
        #3
        This post is deleted!
        G 1 Reply Last reply 22 Aug 2016, 14:44
        0
        • G Offline
          G Offline
          gfxx
          wrote on 22 Aug 2016, 14:42 last edited by
          #4

          @the_

          "Driver not loaded Driver not loaded"
          

          why?????

          Regards
          Giorgio

          bkt

          1 Reply Last reply
          0
          • V VRonin
            22 Aug 2016, 12:51

            This post is deleted!

            G Offline
            G Offline
            gfxx
            wrote on 22 Aug 2016, 14:44 last edited by
            #5

            @VRonin

            Thanks for your interest .... but having a personal translator does not help my poor English ....

            Regards
            Giorgio

            bkt

            1 Reply Last reply
            0
            • V Offline
              V Offline
              VRonin
              wrote on 22 Aug 2016, 14:45 last edited by
              #6

              Did you deploy the driver plugin? qsqlite.dll

              1 Reply Last reply
              1
              • G Offline
                G Offline
                gfxx
                wrote on 23 Aug 2016, 08:59 last edited by
                #7

                I'm on linux .....

                Regards
                Giorgio

                bkt

                1 Reply Last reply
                0
                • G Offline
                  G Offline
                  gfxx
                  wrote on 23 Aug 2016, 09:07 last edited by gfxx
                  #8

                  Pieces of code when I open my db...

                  QPluginLoader loader("/home/myhome/Qt/5.6/gcc_64/plugins/sqldrivers/libqsqlite.so");
                                  loader.load();
                                  qDebug() << loader.errorString(); /*1 mess*/
                  
                  
                  QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                  //QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
                                  db.setDatabaseName( "/home/myhome/Qt/BDB/mydb.sqlite" );
                                   
                   if( !db.open() )
                                      {
                                                 /*my ERROR CONNECTION messages*/
                                      }
                  else{
                                          qDebug() << db.lastError().text();/*2 mess*/
                                      }
                                       qDebug() << db.lastError().text();/*3 mess*/
                  
                                      qDebug ( )  <<  QSqlDatabase::drivers();/*4 mess*/
                  

                  debug messages .....

                  /*1 mess*/ "Unknown error"
                  /*2 mess*/ " "
                  /*3 mess*/ " "
                  /*4 mess*/ ("QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7")
                  

                  bkt

                  W 1 Reply Last reply 23 Aug 2016, 09:40
                  0
                  • G gfxx
                    23 Aug 2016, 09:07

                    Pieces of code when I open my db...

                    QPluginLoader loader("/home/myhome/Qt/5.6/gcc_64/plugins/sqldrivers/libqsqlite.so");
                                    loader.load();
                                    qDebug() << loader.errorString(); /*1 mess*/
                    
                    
                    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                    //QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
                                    db.setDatabaseName( "/home/myhome/Qt/BDB/mydb.sqlite" );
                                     
                     if( !db.open() )
                                        {
                                                   /*my ERROR CONNECTION messages*/
                                        }
                    else{
                                            qDebug() << db.lastError().text();/*2 mess*/
                                        }
                                         qDebug() << db.lastError().text();/*3 mess*/
                    
                                        qDebug ( )  <<  QSqlDatabase::drivers();/*4 mess*/
                    

                    debug messages .....

                    /*1 mess*/ "Unknown error"
                    /*2 mess*/ " "
                    /*3 mess*/ " "
                    /*4 mess*/ ("QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7")
                    
                    W Offline
                    W Offline
                    Wurgl
                    wrote on 23 Aug 2016, 09:40 last edited by Wurgl
                    #9

                    @gfxx QSqlDatabase("QSQLITE") ist enough. No QPluginloader is needed.

                    Of course, yo need to set the name of the database (with sqlite it is the filename)
                    setDatabaseName("your-db-name.sqlite");
                    and you need to open() the database.

                    1 Reply Last reply
                    1
                    • G Offline
                      G Offline
                      gfxx
                      wrote on 23 Aug 2016, 10:14 last edited by gfxx
                      #10
                      /*QPluginLoader loader("/home/myhome/Qt/5.6/gcc_64/plugins/sqldrivers/libqsqlite.so");
                                      loader.load();
                                      qDebug() << loader.errorString(); */
                      
                      
                      QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
                      //QSqlDatabase(QSqlDatabase::addDatabase("QSQLITE"));
                                      db.setDatabaseName( "/home/myhome/Qt/BDB/mydb.sqlite" ); /*<---- 
                      not work I've created mydb with Sqliteman ... 
                      and save it without extension ... so work only if I write "/home/myhome/Qt/BDB/mydb"  */
                                       
                       if( !db.open() )
                                          {
                                                     /*my ERROR CONNECTION messages*/
                                          }
                      else{
                                              qDebug() << db.lastError().text();/*2 mess*/
                                          }
                                           qDebug() << db.lastError().text();/*3 mess*/
                      
                                          qDebug ( )  <<  QSqlDatabase::drivers();/*4 mess*/
                      
                      

                      with mydb.sqlite obtain these error:

                      QSqlQuery::value: not positioned on a valid record
                      QSqlQuery::value: not positioned on a valid record ....
                      

                      regards
                      Giorgio

                      bkt

                      1 Reply Last reply
                      0
                      • G Offline
                        G Offline
                        gfxx
                        wrote on 24 Aug 2016, 10:24 last edited by
                        #11

                        no new with suggested way..

                        regards
                        Giorgio

                        bkt

                        mrjjM 1 Reply Last reply 24 Aug 2016, 10:34
                        0
                        • G gfxx
                          24 Aug 2016, 10:24

                          no new with suggested way..

                          regards
                          Giorgio

                          mrjjM Offline
                          mrjjM Offline
                          mrjj
                          Lifetime Qt Champion
                          wrote on 24 Aug 2016, 10:34 last edited by
                          #12

                          @gfxx said:

                          QSqlQuery::value: not positioned on a valid record ....

                          That could mean you try to read from QSqlQuery before calling next/ check if it is valid.
                          Please look at doc and samples how to use it correctly
                          http://doc.qt.io/qt-5/qsqlquery.html

                            QSqlQuery query("SELECT country FROM artist");
                              while (query.next()) {
                                  QString country = query.value(0).toString();
                                  doSomething(country);
                              }
                          
                          1 Reply Last reply
                          1
                          • G Offline
                            G Offline
                            gfxx
                            wrote on 24 Aug 2016, 10:38 last edited by gfxx
                            #13
                            QSqlDatabase db;
                            
                                QSqlQuery query2(db);
                                query2.prepare("SELECT MAX(id_stc) FROM stc ");
                                query2.exec();
                                query2.next();
                                index2 = query2.value(0).toInt();
                                query2.clear();
                            db.commit();
                            

                            this is my query ...

                            I'm sorry I can not tell the difference with this, my query ...

                            regards
                            Giorgio

                            bkt

                            1 Reply Last reply
                            0
                            • mrjjM Offline
                              mrjjM Offline
                              mrjj
                              Lifetime Qt Champion
                              wrote on 24 Aug 2016, 10:44 last edited by
                              #14

                              well check return value of next() and exec()
                              if false, there is no record.

                              qDebug << "exec says:" << query2.exec();
                              qDebug << "next says:" << query2.next();

                              also use
                              http://doc.qt.io/qt-5/qsqlquery.html#size

                              to see how many u got from the SELECT.

                              The code seems to lack all error handling or this is just simplified code to show?

                              G 2 Replies Last reply 24 Aug 2016, 11:09
                              1
                              • mrjjM mrjj
                                24 Aug 2016, 10:44

                                well check return value of next() and exec()
                                if false, there is no record.

                                qDebug << "exec says:" << query2.exec();
                                qDebug << "next says:" << query2.next();

                                also use
                                http://doc.qt.io/qt-5/qsqlquery.html#size

                                to see how many u got from the SELECT.

                                The code seems to lack all error handling or this is just simplified code to show?

                                G Offline
                                G Offline
                                gfxx
                                wrote on 24 Aug 2016, 11:09 last edited by gfxx
                                #15

                                @mrjj

                                this is just simplified code to show

                                I try to insert qDebug on query exec and next ... also I try to change all queryxx.clear into .finish as suggested on this doc:
                                link http://doc.qt.io/qt-5/qsqlquery.html

                                regards
                                giorgio

                                bkt

                                1 Reply Last reply
                                1
                                • mrjjM mrjj
                                  24 Aug 2016, 10:44

                                  well check return value of next() and exec()
                                  if false, there is no record.

                                  qDebug << "exec says:" << query2.exec();
                                  qDebug << "next says:" << query2.next();

                                  also use
                                  http://doc.qt.io/qt-5/qsqlquery.html#size

                                  to see how many u got from the SELECT.

                                  The code seems to lack all error handling or this is just simplified code to show?

                                  G Offline
                                  G Offline
                                  gfxx
                                  wrote on 24 Aug 2016, 11:38 last edited by
                                  #16

                                  @mrjj

                                  I emphasize this: SQlite Statement is correctly executed .... but I'm unable to make "SELECT MAX(my_id_table)....." ...select the result is relative to the last program session ... INSERT the current session are not seen ...

                                  My code:

                                  void mainwindow::xxx()
                                  {
                                  
                                  int frq1, frq2, frq4, tm1, tm2, tm3, tm4, tm5, tm6, tm7, tm8;
                                  
                                  QSqlQuery Csetp(db);
                                        Csetp.prepare("SELECT fq1, fq2, fq4, t1, t2, t3, t4 ,t5, t6, t7, t8 FROM defaultdata WHERE id_default =:id_default");
                                        Csetp.bindValue(":id_default", programNU);
                                        Csetp.exec();
                                        Csetp.next();
                                        frq1 = Csetp.value(0).toInt();
                                        frq2 = Csetp.value(1).toInt();
                                        frq4 = Csetp.value(2).toInt();
                                        tm1 = Csetp.value(3).toInt();
                                        tm2 = Csetp.value(4).toInt();
                                        tm3 = Csetp.value(5).toInt();
                                        tm4 = Csetp.value(6).toInt();
                                        tm5 = Csetp.value(7).toInt();
                                        tm6 = Csetp.value(8).toInt();
                                        tm7 = Csetp.value(9).toInt();
                                        tm8 = Csetp.value(10).toInt();
                                        //db.commit();
                                        Csetp.finish();
                                        db.commit();
                                  
                                        qDebug() << "Csetp.exec():   " << Csetp.exec();
                                        qDebug() << "Csetp.next():   " << Csetp.next();
                                        qDebug() << "error:   " << Csetp.lastError().text();
                                  
                                  /*do somethings ... */
                                  }
                                  
                                  void MainWindow::modifyPgr(int NRPGR, int f1M, int f2M, int f4M, int t1M, int t2M, int t3M, int t4M, int t5M, int t6M, int t7M, int t8M)
                                  {
                                  
                                      QSqlDatabase db;
                                      QSqlQuery queryM(db);
                                      queryM.prepare("UPDATE defaultdata SET id_default=:id_default, fq1=:fq1, fq2=:fq2, fq4=:fq4, t1=:t1, t2=:t2, t3=:t3, t4=:t4, t5=:t5, t6=:t6, t7=:t7, t8=:t8 WHERE id_default =:id_default");
                                      queryM.bindValue(":id_default", NRPGR );
                                      queryM.bindValue(":fq1",f1M);
                                      queryM.bindValue(":fq2",f2M);
                                      queryM.bindValue(":fq4",f4M);
                                      queryM.bindValue(":t1",t1M);
                                      queryM.bindValue(":t2",t2M);
                                      queryM.bindValue(":t3",t3M);
                                      queryM.bindValue(":t4",t4M);
                                      queryM.bindValue(":t5",t5M);
                                      queryM.bindValue(":t6",t6M);
                                      queryM.bindValue(":t7",t7M);
                                      queryM.bindValue(":t8",t8M);
                                      queryM.exec();
                                      queryM.next();
                                      //db.commit();
                                      queryM.finish();
                                      db.commit();
                                      qDebug() << "queryM:  " << queryM.lastError().text();
                                      qDebug() <<  "queryM-db: " << db.lastError().text();
                                  
                                  /* do somethings ....*/
                                  }
                                  
                                  
                                  *************************App Output*******************
                                  
                                  queryM:   " "
                                  queryM-db:  "Driver not loaded Driver not loaded"
                                  Csetp.exec():    true
                                  Csetp.next():    true
                                  error:    " "
                                  Csetp.exec():    true
                                  Csetp.next():    true
                                  error:    " "
                                  queryM:   " "
                                  queryM-db:  "Driver not loaded Driver not loaded"
                                  

                                  not understand -> "Driver not loaded Driver not loaded" ... library .so is loaded ... qtcreator plugin folder contain libqsqlite .so ... in my .pro file I add targhet to plugin folder of GCC64 containing libqsqlite .so .. I've these declaration on head:

                                  QT       += widgets\
                                  network widgets \
                                  qml quick\
                                  serialbus\
                                  concurrent widgets\
                                  sql\
                                  core\
                                  xml\
                                  opengl\
                                  gui
                                  

                                  not see where is the problem ....

                                  Regards
                                  Giorgio

                                  bkt

                                  1 Reply Last reply
                                  0
                                  • mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on 24 Aug 2016, 11:56 last edited by
                                    #17

                                    Ok, but you check in a funny way
                                    ...
                                    Csetp.bindValue(":id_default", programNU);
                                    Csetp.exec(); <<< where is the check here?

                                    later u do
                                    qDebug() << "Csetp.exec(): " << Csetp.exec();

                                    but thats another exec() so its not really solid.

                                    also you call db.commit(); but where is the start of it ?
                                    http://doc.qt.io/qt-4.8/qsqldatabase.html#commit

                                    but maybe it all comes from
                                    "Driver not loaded Driver not loaded"
                                    Normally , you do not link to any extern .SO files to use SQL.
                                    QT += SQL should be enough :)

                                    G 1 Reply Last reply 24 Aug 2016, 12:10
                                    1
                                    • mrjjM mrjj
                                      24 Aug 2016, 11:56

                                      Ok, but you check in a funny way
                                      ...
                                      Csetp.bindValue(":id_default", programNU);
                                      Csetp.exec(); <<< where is the check here?

                                      later u do
                                      qDebug() << "Csetp.exec(): " << Csetp.exec();

                                      but thats another exec() so its not really solid.

                                      also you call db.commit(); but where is the start of it ?
                                      http://doc.qt.io/qt-4.8/qsqldatabase.html#commit

                                      but maybe it all comes from
                                      "Driver not loaded Driver not loaded"
                                      Normally , you do not link to any extern .SO files to use SQL.
                                      QT += SQL should be enough :)

                                      G Offline
                                      G Offline
                                      gfxx
                                      wrote on 24 Aug 2016, 12:10 last edited by
                                      #18

                                      @mrjj ```
                                      QSqlDatabase db;
                                      QSqlQuery Csetp(db);
                                      Csetp.prepare("SELECT fq1, fq2, fq4, t1, t2, t3, t4 ,t5, t6, t7, t8 FROM defaultdata WHERE id_default =:id_default");
                                      Csetp.bindValue(":id_default", programNU);
                                      qDebug() << "Csetp.exec(): " << Csetp.exec();
                                      qDebug() << "Csetp.next(): " << Csetp.next();
                                      qDebug() << "error: " << Csetp.lastError().text();
                                      frq1 = Csetp.value(0).toInt();
                                      frq2 = Csetp.value(1).toInt();
                                      frq4 = Csetp.value(2).toInt();
                                      tm1 = Csetp.value(3).toInt();
                                      tm2 = Csetp.value(4).toInt();
                                      tm3 = Csetp.value(5).toInt();
                                      tm4 = Csetp.value(6).toInt();
                                      tm5 = Csetp.value(7).toInt();
                                      tm6 = Csetp.value(8).toInt();
                                      tm7 = Csetp.value(9).toInt();
                                      tm8 = Csetp.value(10).toInt();
                                      //db.commit();
                                      Csetp.finish();
                                      db.commit();

                                      *****output

                                      Csetp.exec(): true
                                      Csetp.next(): true
                                      error: " "

                                      
                                      you are in right ... but result not change..
                                      
                                      Regards
                                      Giorgio

                                      bkt

                                      1 Reply Last reply
                                      0
                                      • G Offline
                                        G Offline
                                        gfxx
                                        wrote on 24 Aug 2016, 12:12 last edited by gfxx
                                        #19
                                        QSqlDatabase db;
                                              QSqlQuery Csetp(db);
                                              Csetp.prepare("SELECT fq1, fq2, fq4, t1, t2, t3, t4 ,t5, t6, t7, t8 FROM defaultdata WHERE id_default =:id_default");
                                              Csetp.bindValue(":id_default", programNU);
                                              qDebug() << "Csetp.exec():   " << Csetp.exec();
                                              qDebug() << "Csetp.next():   " << Csetp.next();
                                              qDebug() << "error:   " << Csetp.lastError().text();
                                              frq1 = Csetp.value(0).toInt();
                                              frq2 = Csetp.value(1).toInt();
                                              frq4 = Csetp.value(2).toInt();
                                              tm1 = Csetp.value(3).toInt();
                                              tm2 = Csetp.value(4).toInt();
                                              tm3 = Csetp.value(5).toInt();
                                              tm4 = Csetp.value(6).toInt();
                                              tm5 = Csetp.value(7).toInt();
                                              tm6 = Csetp.value(8).toInt();
                                              tm7 = Csetp.value(9).toInt();
                                              tm8 = Csetp.value(10).toInt();
                                              //db.commit();
                                              Csetp.finish();
                                              //db.commit();
                                              qDebug() << "error db on Csetp:   " << db.lastError().text();
                                        
                                        ***********************output without commit()****************
                                        Csetp.exec():    true
                                        Csetp.next():    true
                                        error:    " "
                                        error db on Csetp:    "Driver not loaded Driver not loaded"
                                        

                                        add INCLUDEPATH at libsqlite.so is my last chance to try to solve to myself the "impossible error": "Driver not loaded Driver not loaded" ....

                                        but now I try community help...

                                        Regards
                                        Giorgio

                                        bkt

                                        1 Reply Last reply
                                        0
                                        • mrjjM Offline
                                          mrjjM Offline
                                          mrjj
                                          Lifetime Qt Champion
                                          wrote on 24 Aug 2016, 12:25 last edited by
                                          #20

                                          Hi
                                          did you also check with

                                          qDebug() << "number of rows: " << Csetp.size();

                                          just to know?

                                          G 1 Reply Last reply 24 Aug 2016, 12:57
                                          0

                                          1/24

                                          22 Aug 2016, 11:37

                                          • Login

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