Results 1 to 5 of 5

Thread: QT 5.1 Mysql Transaction

  1. #1
    Join Date
    Feb 2013
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.1 Mysql Transaction

    Hello there,

    i have some mysql qt problems i dont understand. First some speccs: mysql 5.6, qt 5.1, windows7 and ubuntu 12.04. Inno db table.

    this simple snippet starts a transaction, inserted data into table1 and commit the data. But the problem is the data will be inserted twice - i cant figure it out why this happens.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("dbOne"); //is already opened and all works fine...
    2. qDebug() << db.transaction(); // returns true
    3. QSqlQuery qry("SET autocommit = 0",db); // test purpose
    4. QSqlQuery qry2("INSERT INTO table1(col1) VALUES(1)",db);
    5. if (!qry.exec()){ // returns true
    6. qDebug() << qry.lastError();
    7. }else{
    8. qDebug() << qry2.exec();//returns true
    9. qDebug() << db.commit() << db.lastError().text(); // commit returns true and last error string is empty, But the Data is inserted twice, why?
    10. }
    To copy to clipboard, switch view to plain text mode 

    If I try the same thing with a store_procedure and wrap it with the code above (instead of exec query, call storeProc) - the data will be inserted twice. If I remove the qry.exec() line the data is only inserted once.

    Another problem when I use a storeProcedure with many inserts on different tables and in a loop in c++ qt the commit() (outside the loop - transaction starts before the loop and commit after the loop) returns an error after i try to commit the data: "commands out of sync you can't run this command now".

    I Dont get the point what happens here. A Simple insert, a simple transaction and two behaviour i didnt get...
    Thanks for any kind of help!

    best regards
    ane


    Added after 11 minutes:


    If I ry this in mysql workbench all is fine...
    start transaction;
    INSERT INTO table1(col1) VALUES(1)
    commit; # or rollback all works fine
    Last edited by Anenja; 8th October 2013 at 12:31.

  2. #2
    Join Date
    Dec 2012
    Posts
    197
    Thanks
    25
    Thanked 41 Times in 33 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.1 Mysql Transaction

    Hello,
    Quote Originally Posted by qt-project documentation
    QSqlQuery::QSqlQuery ( const QString & query = QString(), QSqlDatabase db = QSqlDatabase() )
    Constructs a QSqlQuery object using the SQL query and the database db. If db is not specified, or is invalid, the application's default database is used. If query is not an empty string, it will be executed.
    So apparently you are executing the query twice hence the two inserts happen.
    Check this link QSqlQuery
    Good luck.
    Last edited by toufic.dbouk; 8th October 2013 at 14:17.

  3. The following user says thank you to toufic.dbouk for this useful post:

    Anenja (8th October 2013)

  4. #3
    Join Date
    Feb 2013
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.1 Mysql Transaction

    Oh thanks for this information - i really missed this in the docs.
    Really big Thanks! This only works for updates/inserts the cTor execute. For Select it doesn´t work... really confusing but anyway,..

    Is it possible to create a transaction for a loop? S.th like this.
    Qt Code:
    1. (this code wont run just for demonstrate purpose)
    2. qDebug() << db.transaction();
    3. QSqlQuery qry("SET autocommit = 0",db);
    4. bool allOk = false;
    5. // start loop and insert some data
    6. for (int i = 0; i < 5; ++i){
    7. QSqlQuery qry2("INSERT INTO table1(col1) VALUES(i)",db);
    8. int newID = qry2.lastInsertId();
    9. qry2.exec("CALL greatProc(newID)");
    10. // get return value of store proc and run again
    11. if (storeProcRetVal)
    12. allOk = true;
    13. else{
    14. allOk = false; // error and we are done
    15. beak;
    16. }
    17. }
    18.  
    19. // looping finished commit or rollback
    20. if (allOk)
    21. db.commit();
    22. else
    23. db.rollback();
    To copy to clipboard, switch view to plain text mode 

    Thanks again for any kind of help!
    ane

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QT 5.1 Mysql Transaction

    Starting a transaction with MySql will automatically disable autocommit, so you might as well remove all the autocommit stuff.

    QSqlQuery::prepare() outside the loop and QSqlQuery::bindValue() inside the loop.

  6. #5
    Join Date
    Feb 2013
    Posts
    19
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QT 5.1 Mysql Transaction

    Thanks for ur reply. I give it a try and post the result.

Similar Threads

  1. QIBASE and problem with transaction
    By Hostel in forum Qt Programming
    Replies: 0
    Last Post: 21st May 2013, 12:01
  2. Postgresql transaction
    By matteo.ceruti in forum Qt Programming
    Replies: 2
    Last Post: 13th May 2012, 17:24
  3. Slow insert with Transaction?!
    By codeman in forum Qt Programming
    Replies: 4
    Last Post: 20th October 2010, 10:38
  4. Replies: 1
    Last Post: 11th March 2010, 17:42
  5. QDatabase::transaction()
    By sunil.thaha in forum Qt Programming
    Replies: 2
    Last Post: 3rd February 2006, 13:15

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.