Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Something really strange with QSqlQuery and DB



  • 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


  • Lifetime Qt Champion

    Hi,

    Yes, you should show your code.



  • 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



  • @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?!


  • Lifetime Qt Champion

    @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...



  • @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..


Log in to reply