Results 1 to 7 of 7

Thread: SQLite - QSqlDatabase::transaction()

  1. #1
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question SQLite - QSqlDatabase::transaction()

    Hi,

    how can i test if a TRANSACTION is ok?

    I try this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. QSqlQuery q(db);
    3. qDebug() << db.driver()->hasFeature(QSqlDriver::Transactions); //true
    To copy to clipboard, switch view to plain text mode 
    so SQLite aided transactions

    But the COMMIT for a TRANSACTION is always TRUE. Here a example:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.transaction();
    3.  
    4. qDebug() << q.exec("SELECT;"); //false
    5.  
    6. q.clear();
    7. if(!db.commit()){
    8. db.rollback();
    9. }
    To copy to clipboard, switch view to plain text mode 
    The COMMIT is TRUE and i try this:
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. QSqlQuery q(db);
    3. qDebug() << db.driver()->beginTransaction(); //true
    4. qDebug() << q.exec("SELECT;"); //false
    5. qDebug() << db.driver()->commitTransaction();//true
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. qDebug() << q.exec("BEGIN TRANSACTION"); //true
    2. qDebug() << q.exec("SELECT;"); //false
    3. qDebug() << q.exec("COMMIT");//true
    To copy to clipboard, switch view to plain text mode 
    The ROLLBACK works fine, but i cant verifying the COMMIT respectively the TRANSACTION!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLite - QSqlDatabase::transaction()

    An incorrect select statement won't fail a transaction. Only if you have another connection to the database and do an insert or update there on any of the rows modified within the transaction will cause it to auto-abort. In other cases you have to roll it back yourself (for instance if the user cancels the task in progress).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    whitefurrows (5th May 2009)

  4. #3
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: SQLite - QSqlDatabase::transaction()

    Now the COMMIT fail and i show a message and do nothing. Is that OK?
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.transaction();
    3.  
    4. if ( !q.exec("SELECT;") )
    5. db.rollback();
    6.  
    7. q.clear();
    8. if(!db.commit())
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: SQLite - QSqlDatabase::transaction()

    I don't know what you are asking for but you can't commit a transaction that has already been rolled back.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. The following user says thank you to wysota for this useful post:

    whitefurrows (5th May 2009)

  7. #5
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Question Re: SQLite - QSqlDatabase::transaction()

    I want read a sql-script and rollback if a sql-statement fails. I'm not sure how can i do that right. That's my code:

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.transaction();
    3.  
    4. bool sql_ok=TRUE;
    5. QFile file(:/script.sql);
    6. QString script=file.readAll();
    7. QStringList sql=script.split(";", QString::SkipEmptyParts);
    8.  
    9. for (int i=0; i<sql.count(); i++)
    10. {
    11. sql_ok=q.exec(sql[i]);
    12. if (!sql_ok)
    13. {
    14. QMessageBox::critical(0, "Error", q.lastError().text());
    15. db.rollback();
    16. break;
    17. }
    18. }
    19.  
    20. q.clear();
    21. if(sql_ok)
    22. {
    23. if(!db.commit()){
    24. QMessageBox::critical(0, "Error", db.lastError().text());
    25. db.rollback();
    26. }
    27. }
    To copy to clipboard, switch view to plain text mode 
    Please help me and give me a example how that works right. Thanks in advance.

  8. #6
    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: SQLite - QSqlDatabase::transaction()

    Just simply like this
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database();
    2. db.transaction();
    3.  
    4. bool sql_ok=TRUE;
    5. QFile file(:/script.sql);
    6. QString script=file.readAll();
    7. QStringList sql=script.split(";", QString::SkipEmptyParts);
    8.  
    9. for (int i=0; i<sql.count() && sql_ok; i++)
    10. {
    11. sql_ok=q.exec(sql[i]);
    12. }
    13.  
    14. q.clear();
    15. if(sql_ok)
    16. {
    17. sql_ok = db.commit();
    18. }
    19.  
    20. if(!sql_ok)
    21. {
    22. QMessageBox::critical(0, "Error", q.lastError().text());
    23. db.rollback();
    24. }
    To copy to clipboard, switch view to plain text mode 

  9. The following user says thank you to Lesiok for this useful post:

    whitefurrows (5th May 2009)

  10. #7
    Join Date
    May 2006
    Posts
    108
    Thanks
    35
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Thumbs up Re: SQLite - QSqlDatabase::transaction()

    Thanks for the help, the problem is now solved.

Similar Threads

  1. Replies: 1
    Last Post: 26th March 2009, 16:25
  2. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 20:43
  3. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 15:18
  4. SQLite
    By cyberboy in forum Installation and Deployment
    Replies: 1
    Last Post: 15th April 2008, 20:46
  5. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 15:59

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.