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

When QSqlQuery .exec function called?



  • when i using under code

    [code example] :

    q.prepare();
    ~
    int tracking_id = 0;
    tracking_id ++;
    ~
    if(tracking_id == 2)
    {
    q.bindvalue(":ti", tracking_id);
    }
    ~
    if(q.exec())
    {
    emit send_data_to_center(data);
    }

    ======================================
    end of the running this code
    when i check the db
    i found tracking_id 0,1,2

    why this happend ?

    i thought .exec function only called when exist q.bindvalue ....
    please help me...



  • @donkey007

    When QSqlQuery .exec function called?

    It is called when it executes the if(q.exec()) in your code.

    i thought .exec function only called when exist q.bindvalue ....

    It is called when the line is hit. If you have only called q.bindvalue() when tracking_id == 2 then no value will be bound to :ti when tracking_id != 2, which will presumably cause your query not to work. We don't know because you don't show what your query is.



  • thanks for your comment :)
    it's my query :

    q.prepare("INSERT INTO "TABLE NAME" (id, tracking_id, check)"
    "VALURE (:id, :ti)" "ON CONFLICT (id) DO UPDATE SET tracking_id=:ti");

    so.. can you tell me what is problem is?

    i want to excute the .exec function only "tracking_id == 2"
    but now the .exec function also return true when tracking_id == 0 or 1


  • Lifetime Qt Champion

    @donkey007 said in When QSqlQuery .exec function called?:

    i want to excute the .exec function only "tracking_id == 2"

    Then change your code so it does what you want. Currently you always call exec().



  • now i know whats my problem is thank you sir!


Log in to reply