Results 1 to 2 of 2

Thread: QtSql and Transactions question.

  1. #1
    Join Date
    Sep 2011
    Location
    Buenos Aires
    Posts
    9
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Question QtSql and Transactions question.

    Hi!.
    I have a question related to the correct usage of transactions with QtSql and PostgreSQL.

    If i issue a select on a table wich has a current not commited insert transaction, I understand that no rows should be returned.
    but with the following test code. when i call showTest(); it lists the rows as if they were already commited.

    What i want to archieve is that i have to make changes in the database that need multiple insert,update, and maybe delete queries that need to be made all at once, or none at all if any of the queries fail.
    But i expected to get no results from a select before the commit was made.

    Did I get it al wrong and thats not how one should use transactions?

    The example code i'm using is as follows

    Qt Code:
    1. bool utility::beginTransaction() {
    2.  
    3. QSqlQuery consulta;
    4.  
    5. if(!consulta.exec("BEGIN TRANSACTION")) {
    6. qDebug() << consulta.lastError() << consulta.lastError().type();
    7. return false;
    8. }
    9. else { return true; }
    10. }
    11.  
    12. bool utility::commitTransaction() {
    13.  
    14. QSqlQuery consulta;
    15.  
    16. if(!consulta.exec("COMMIT")) {
    17. qDebug() << consulta.lastError() << consulta.lastError().type();
    18. return false;
    19. }
    20. else { return true; }
    21.  
    22. }
    23.  
    24. u.clearTestTable(); // delete from test;
    25.  
    26. if ( u.beginTransaction() == true ){
    27. qDebug() << "-- Transaction Starts--";
    28.  
    29. qDebug() << u.insertTest(); //insert into test(data) values('somevalue');
    30. qDebug() << u.insertTest();
    31. qDebug() << u.insertTest();
    32. qDebug() << u.insertTest();
    33. qDebug() << u.insertTest();
    34. qDebug() << u.insertTest();
    35.  
    36. qDebug() << "-- inserts are not commited, should print nothing! --";
    37. qDebug() << u.showTest(); //select data from test; //but prints the records as if they were commited already.
    38.  
    39. u.commitTransaction();
    40. qDebug() << "-- After the commit. Now i should see some rows --";
    41. qDebug() << u.showTest(); //select data from test;
    42.  
    43. }
    44. else {
    45. qDebug() << "-- something went wrong!";
    46. }
    To copy to clipboard, switch view to plain text mode 


    Thanks!

    Edit. After re reading the post I realized that this may not be related to Qt at all. I'm sorry, I will check how to delete the thread.
    Last edited by slqh; 12th October 2011 at 06:41.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QtSql and Transactions question.

    You should read about SQL transactions and isolation levels.

Similar Threads

  1. QtSql or QtSql/ ?
    By szisziszilvi in forum Newbie
    Replies: 3
    Last Post: 8th March 2011, 15:44
  2. Using transactions
    By Jeffb in forum Newbie
    Replies: 2
    Last Post: 27th April 2010, 13:44
  3. SQL transactions with QSqlQueryModel/QTableView
    By estanisgeyer in forum Qt Programming
    Replies: 2
    Last Post: 15th June 2009, 18:52
  4. SQLITE Transactions
    By mcosta in forum Qt Programming
    Replies: 4
    Last Post: 7th April 2009, 19:45
  5. Rollback transactions in QDataTable
    By bubba in forum Qt Programming
    Replies: 0
    Last Post: 25th July 2007, 09:41

Tags for this Thread

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.