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

Application not responding while using "QUERY"



  • Any simple/complex database query

      QString databaseName = getDatabaseName();
        int databasePort = getPort();
        QString databaseHostname = getHostname();
        QString databasePassword = getPassword();
        QString databaseUsername = getUsername();
    
        QString command = "insert into request(user_id,special_value, operation,page) VAlUES('"+user_id+"','"+special_value+"','"+operation+"','"+page+"' );";
        {
            QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL","save_request");
            db.setDatabaseName(databaseName);
            db.setPort(databasePort);
            db.setHostName(databaseHostname);
            db.setUserName(databaseUsername);
            db.setPassword(databasePassword);
    
    
    
            if(db.open()){
                QSqlQuery query(db);
                if(query.exec(command)){
    //saved
                }else{
    //not saved
                }
    
            }else{
                //database not executed
            }
        }
        QSqlDatabase::removeDatabase("save_request");
    

    problem is
    Application shows NOT RESPONDING while querying and again
    RESPONDS back after the query is finished
    It is very fast while using in localhost and ofcourse it will be .
    It shows not responding while going through query
    The program then responds back with data properly

    Database isn't huge so that's not the main problem,
    It contains 10 columns with 20 rows datas (which I guess isn't huge)
    While not in internet connection it immediately shows errors ( // from database not connected block)

    What may be the way to fix it



  • @Thank-You
    Whether a message like "NOT RESPONDING" is shown depends on the OS/windowing system. You do not state what platform you are using.

    If the behaviour you see is not acceptable you would have to move all your database operations over to a separate thread from the UI, and use signals/slots to communicate between them.

    I note that you are creating the database connection each time this code is executed. That could be "slow", taking more time than executing the query. Before you consider a separate thread, the usual pattern is to create the database connection with credentials once somewhere outside of each query and then use an existing connection for queries.



  • @Thank-You said in Application not responding while using "QUERY":

    Database isn't huge so that's not the main problem,
    It contains 10 columns with 20 rows datas (which I guess isn't huge)
    While not in internet connection it immediately shows errors ( // from database not connected block)
    What may be the way to fix it

    You analyse is not totally correct.
    The problem with your code, is that you are not using QSqlDatabase as it have been design.
    You are creating and destroying database connection each time you send SQL commands to DB. That's the wrong way.
    The right way is to define one time the SQL connection and that use the connection with a query to send SQL commands to DB.



  • @JonB said in Application not responding while using "QUERY":
    I am on windows operating system
    I agree with what you said
    Although I don't know much about threads so I will try to look at it

    efore you consider a separate thread, the usual pattern is to create the database connection with credentials once somewhere outside of each query

    I will see this.
    "You suggested me on the last question I asked " So I have implemented in almost all cases i am implementing it and will say you after I complete this


  • Lifetime Qt Champion

    We already had the discussion on how to properly use a QSqlDatabase connection here but now you again simply ignore what we told you there...


Log in to reply