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

send two statements in a query or two separated queries?



  • 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


  • Lifetime Qt Champion

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



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



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


  • Lifetime Qt Champion

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



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



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


  • Lifetime Qt Champion

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


  • Banned

    This post is deleted!

  • Banned

    This post is deleted!

  • Banned

    This post is deleted!

Log in to reply