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. Application not responding while using "QUERY"
Forum Updated to NodeBB v4.3 + New Features

Application not responding while using "QUERY"

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 4 Posters 424 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.
  • Thank YouT Offline
    Thank YouT Offline
    Thank You
    wrote on last edited by
    #1

    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

    Let's make QT free or It will go forever

    TRUE AND FALSE <3

    JonBJ KroMignonK 2 Replies Last reply
    0
    • Thank YouT Thank You

      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

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

      @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 YouT 1 Reply Last reply
      1
      • Thank YouT Thank You

        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

        KroMignonK Offline
        KroMignonK Offline
        KroMignon
        wrote on last edited by
        #3

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

        It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

        1 Reply Last reply
        2
        • JonBJ JonB

          @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 YouT Offline
          Thank YouT Offline
          Thank You
          wrote on last edited by
          #4

          @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

          Let's make QT free or It will go forever

          TRUE AND FALSE <3

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

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

            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
            4

            • Login

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