Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. The Lounge
  4. send two statements in a query or two separated queries?
QtWS25 Last Chance

send two statements in a query or two separated queries?

Scheduled Pinned Locked Moved Unsolved The Lounge
11 Posts 8 Posters 1.6k Views
  • 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!

    it is posible to write X sql statements in one query?.. i need to set some keys for X fields so i will not receive any value from server …

    And.. is doing like this way better for gain performance compared to doing X single queries?

    Thanks

    jsulmJ JonBJ 2 Replies Last reply
    0
    • U U7Development

      HEllo!

      it is posible to write X sql statements in one query?.. i need to set some keys for X fields so i will not receive any value from server …

      And.. is doing like this way better for gain performance compared to doing X single queries?

      Thanks

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @U7Development said in send two statements in a query or two separated queries?:

      i need to set some keys for X fields

      Without knowing your use case hard to say anything.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • U U7Development

        HEllo!

        it is posible to write X sql statements in one query?.. i need to set some keys for X fields so i will not receive any value from server …

        And.. is doing like this way better for gain performance compared to doing X single queries?

        Thanks

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by
        #3

        @U7Development
        It's not possible to say if you don't think telling us what the RDBMS is might be relevant....

        1 Reply Last reply
        0
        • U Offline
          U Offline
          U7Development
          wrote on last edited by U7Development
          #4

          thanks for answering..

          what i need is to set two different keys:

          1. One statement per query in one function
          void some_where_function(){
               set_my_keys("update tkeys set used=1 where home_id=0");
               set_my_keys("update tkeys set used=0 where home_id=1");
          }
          
          void set_my_keys(const QString _s){
               QSqlDatabase& db = [get here a reference to my db];
               QSqlQuery q(db);
          
               if (!q.exec(_s))  //throw some error message 
          }
          
          1. Two statements on two queries in just one function
          void some_where_function(){
               set_my_keys_2();
          }
          
          void set_my_keys_2(){
               QSqlDatabase& db = [get here a reference to my db];
          
               QString txt = QString("update tkeys set used=1 where home_id=0;");
               QString txt2 = QString("update tkeys set used=0 where home_id=1;");
          
               QSqlQuery q(db);
          
               if (!q.exec(txt))  //throw some error message 
               if (!q.exec(txt2)) //throw some error message
          }
          
          
          1. Concatenate statements and send them into a single query
            void some_where_function(){
            set_my_keys_3();
            }

          void set_my_keys_3(){
          QSqlDatabase& db = [get here a reference to my db];

           QString txt = QString("update tkeys set used=1 where home_id=0;");
           QString txt2 = QString("update tkeys set used=0 where home_id=1;");
          
           QSqlQuery q(db);
          
           if (!q.exec(txt + txt2))  //throw some error message 
          

          }

          Is there any significant performance difference between them?
          I know there is no that big significant difference with just 2 statements.. but i'm looking to send at least 6 or 7 statements.. and you know.. seems to be some strange executing the "send_my_keys()" function 7 times.. don't you think?..
          So i was thinking in concatenate those 7 keys in just one query.. (way 3)

          Maybe there is another way?

          Thanks again.

          jsulmJ 1 Reply Last reply
          0
          • U U7Development

            thanks for answering..

            what i need is to set two different keys:

            1. One statement per query in one function
            void some_where_function(){
                 set_my_keys("update tkeys set used=1 where home_id=0");
                 set_my_keys("update tkeys set used=0 where home_id=1");
            }
            
            void set_my_keys(const QString _s){
                 QSqlDatabase& db = [get here a reference to my db];
                 QSqlQuery q(db);
            
                 if (!q.exec(_s))  //throw some error message 
            }
            
            1. Two statements on two queries in just one function
            void some_where_function(){
                 set_my_keys_2();
            }
            
            void set_my_keys_2(){
                 QSqlDatabase& db = [get here a reference to my db];
            
                 QString txt = QString("update tkeys set used=1 where home_id=0;");
                 QString txt2 = QString("update tkeys set used=0 where home_id=1;");
            
                 QSqlQuery q(db);
            
                 if (!q.exec(txt))  //throw some error message 
                 if (!q.exec(txt2)) //throw some error message
            }
            
            
            1. Concatenate statements and send them into a single query
              void some_where_function(){
              set_my_keys_3();
              }

            void set_my_keys_3(){
            QSqlDatabase& db = [get here a reference to my db];

             QString txt = QString("update tkeys set used=1 where home_id=0;");
             QString txt2 = QString("update tkeys set used=0 where home_id=1;");
            
             QSqlQuery q(db);
            
             if (!q.exec(txt + txt2))  //throw some error message 
            

            }

            Is there any significant performance difference between them?
            I know there is no that big significant difference with just 2 statements.. but i'm looking to send at least 6 or 7 statements.. and you know.. seems to be some strange executing the "send_my_keys()" function 7 times.. don't you think?..
            So i was thinking in concatenate those 7 keys in just one query.. (way 3)

            Maybe there is another way?

            Thanks again.

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @U7Development said in send two statements in a query or two separated queries?:

            6 or 7 statements

            I really would not care about sending 6/7 queries.

            "strange executing the "send_my_keys()" function 7 times.. don't you think?.." - do you mean set_my_keys()? Why is it strange to execute a function several times? Also, you could use your second approach and call it once to send several queries.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            U 1 Reply Last reply
            0
            • jsulmJ jsulm

              @U7Development said in send two statements in a query or two separated queries?:

              6 or 7 statements

              I really would not care about sending 6/7 queries.

              "strange executing the "send_my_keys()" function 7 times.. don't you think?.." - do you mean set_my_keys()? Why is it strange to execute a function several times? Also, you could use your second approach and call it once to send several queries.

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

              @jsulm thanks
              I was meaning that is strange or let say some kind of absurd the way 1 because:

              Hello db
              , please set my key A to 1
              Bye db

              Hello again db
              , please set my key B to 0
              Bye bd

              Rather than saying:
              Hello db
              , please set my key A to 1
              , please set my key B to 1
              Bye db...

              I guess second one is more elegant isnt?

              S 1 Reply Last reply
              0
              • Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote on last edited by
                #7

                @U7Development said in send two statements in a query or two separated queries?:

                t is posible to write X sql statements in one query?

                When you want/need to do multiple queries in one step that's called a "transaction". It groups the queries into an atomic operation so that the behavior is consistent. Does your db support transactions? and for efficiency (if your database supports it) you should use "prepared statements". Those are queries stored on the database server that can be executed more efficiently than being manually created every time you need to execute them.

                1 Reply Last reply
                1
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @Kent-Dorfman said in send two statements in a query or two separated queries?:

                  Those are queries stored on the database server that can be executed more efficiently than being manually created every time you need to execute them.

                  This is only really true (and measurable) when you execute the query more than once which is not the case here.

                  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
                  1
                  • U U7Development

                    @jsulm thanks
                    I was meaning that is strange or let say some kind of absurd the way 1 because:

                    Hello db
                    , please set my key A to 1
                    Bye db

                    Hello again db
                    , please set my key B to 0
                    Bye bd

                    Rather than saying:
                    Hello db
                    , please set my key A to 1
                    , please set my key B to 1
                    Bye db...

                    I guess second one is more elegant isnt?

                    S Offline
                    S Offline
                    Stroman
                    Banned
                    wrote on last edited by Stroman
                    #9
                    This post is deleted!
                    1 Reply Last reply
                    0
                    • A Offline
                      A Offline
                      Arthur774
                      Banned
                      wrote on last edited by
                      #10
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • A Offline
                        A Offline
                        Anker79
                        Banned
                        wrote on last edited by
                        #11
                        This post is deleted!
                        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