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. Something really strange with QSqlQuery and DB
Forum Updated to NodeBB v4.3 + New Features

Something really strange with QSqlQuery and DB

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 294 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.
  • U Offline
    U Offline
    U7Development
    wrote on last edited by
    #1

    Hello !.. happy xmass...

    Im developing an inventory program, it is really simple, it asks you to input locations, there is a class called location.h which cointains data about location, all locations are stored into a map where first type is an integer (unique antoincremented and not null)

    When i click on OK button it adds the current information into a pair, then that pair is inserted into the map, at the same time...

    When user closes the program, it needs to save every map register to DB.
    i iterate each register on the map using iterator pointer..

    The magic problem:
    When i add just one and no more register to BD, everything works fine...
    But when i add two or more consecutive registers to BD, then the error comes up : "QSqlQuery::prepare: database not open"

    It is exactly the same alghorithm for 1 or more registers...
    (i can show my code if needed)

    Thanks in advance

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

      Hi,

      Yes, you should show your code.

      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
      • U Offline
        U Offline
        U7Development
        wrote on last edited by U7Development
        #3

        there is a dialog window called wubicaciones, it has a QDialogButtonBox that has signal, OK for accepted() and Cancel for rejected(), this window has 3 QLineEdit widgets where each value is taken into a mubicacion.h class, then the map is std::map<int, mubicacion>, this map is allocated into a singleton object.

        This is the function that executes when user presses OK

        void wubicaciones::onClick_bboxOK(){
            sdata* singleton = sdata::get_instancia();
            std::map<int, mubicacion>& tmp_ubicaciones = singleton->get_tmpUbicaciones();
        
            if (tmp_ubicaciones.size() > 0){
                QSqlDatabase db = singleton->get_db();
        
                if (!db.open()) Utils::print_sqlConnection_error_and_exit(db, "Excuse me, i could not connet to DB.\nE: ");
        
                for (std::map<int, mubicacion>::iterator it = tmp_ubicaciones.begin(); it != tmp_ubicaciones.end(); ++it){
                    
                    QString C_TXTCONSULTA = "INSERT INTO tUbicaciones (nombre, direccion, telefono) values (:nom, :dir, :tel)";
                    QSqlQuery consulta(db);
                    consulta.prepare(C_TXTCONSULTA);
                    consulta.bindValue(":nom", it->second.get_nombre());
                    consulta.bindValue(":dir", it->second.get_direccion());
                    consulta.bindValue(":tel", it->second.get_telefono());
        
                    //This triggers when 2 or more registers were inserted into the map.
                    if (!consulta.exec()) Utils::print_sqlDB_error_and_exit(consulta, "Excuse me, i could not insert into tUbicaciones.\nE: ");
        
                     //at this point, Qt console prints QSqlQuery::prepare db is not connected
        
                    db.close();
                }
            }
        
            accept();
        }
        

        The way it work and not:

        Working way:
        In the wubicaciones dialog window, i fill all three fields and press OK, then i close my program and register is writen into DB successfully….

        Non working way:
        In the wubicaciones dialog window, i fill all three fields and press OK, then press Add and repeat the process for 2 or more registers, then close my program and here is when i get the error... in other words, the errors shows up when i try to save a map that has 2 or more sub-ítems...

        Edit: i have isolated the problem, forget that pair thing, that was made for a second map copy, but in this case it has no sense...

        Thanks

        JonBJ 1 Reply Last reply
        0
        • U U7Development

          there is a dialog window called wubicaciones, it has a QDialogButtonBox that has signal, OK for accepted() and Cancel for rejected(), this window has 3 QLineEdit widgets where each value is taken into a mubicacion.h class, then the map is std::map<int, mubicacion>, this map is allocated into a singleton object.

          This is the function that executes when user presses OK

          void wubicaciones::onClick_bboxOK(){
              sdata* singleton = sdata::get_instancia();
              std::map<int, mubicacion>& tmp_ubicaciones = singleton->get_tmpUbicaciones();
          
              if (tmp_ubicaciones.size() > 0){
                  QSqlDatabase db = singleton->get_db();
          
                  if (!db.open()) Utils::print_sqlConnection_error_and_exit(db, "Excuse me, i could not connet to DB.\nE: ");
          
                  for (std::map<int, mubicacion>::iterator it = tmp_ubicaciones.begin(); it != tmp_ubicaciones.end(); ++it){
                      
                      QString C_TXTCONSULTA = "INSERT INTO tUbicaciones (nombre, direccion, telefono) values (:nom, :dir, :tel)";
                      QSqlQuery consulta(db);
                      consulta.prepare(C_TXTCONSULTA);
                      consulta.bindValue(":nom", it->second.get_nombre());
                      consulta.bindValue(":dir", it->second.get_direccion());
                      consulta.bindValue(":tel", it->second.get_telefono());
          
                      //This triggers when 2 or more registers were inserted into the map.
                      if (!consulta.exec()) Utils::print_sqlDB_error_and_exit(consulta, "Excuse me, i could not insert into tUbicaciones.\nE: ");
          
                       //at this point, Qt console prints QSqlQuery::prepare db is not connected
          
                      db.close();
                  }
              }
          
              accept();
          }
          

          The way it work and not:

          Working way:
          In the wubicaciones dialog window, i fill all three fields and press OK, then i close my program and register is writen into DB successfully….

          Non working way:
          In the wubicaciones dialog window, i fill all three fields and press OK, then press Add and repeat the process for 2 or more registers, then close my program and here is when i get the error... in other words, the errors shows up when i try to save a map that has 2 or more sub-ítems...

          Edit: i have isolated the problem, forget that pair thing, that was made for a second map copy, but in this case it has no sense...

          Thanks

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @U7Development
          You db.open() outside of your iterator loop. However, you db.close() inside your iterator loop. So after the first loop execution the db is closed, and then you try to prepare against it and of course you get the error message. What else would you expect?!

          U 1 Reply Last reply
          4
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @U7Development said in Something really strange with QSqlQuery and DB:

            db.close();

            I would nothing other expect than 'Database is not open' when you closed the database before...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            2
            • JonBJ JonB

              @U7Development
              You db.open() outside of your iterator loop. However, you db.close() inside your iterator loop. So after the first loop execution the db is closed, and then you try to prepare against it and of course you get the error message. What else would you expect?!

              U Offline
              U Offline
              U7Development
              wrote on last edited by
              #6

              @JonB said in Something really strange with QSqlQuery and DB:

              You db.open() outside of your iterator loop. However, you db.close() inside your iterator loop. So after the first loop execution the db is closed, and then you try to prepare against it and of course you get the error message. What else would you expect?!

              LOL!!!!... you are right, good observer...
              Thanks a lot...
              what a blame.. God..

              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