Results 1 to 7 of 7

Thread: SQLite - QSqlDatabase::transaction()

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,018 Times in 4,794 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.


  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()

    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.

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    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 

  6. The following user says thank you to Lesiok 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

    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, 15:25
  2. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 19:43
  3. sqlite version in Qt
    By janus in forum Newbie
    Replies: 4
    Last Post: 5th February 2009, 14:18
  4. SQLite
    By cyberboy in forum Installation and Deployment
    Replies: 1
    Last Post: 15th April 2008, 19:46
  5. sqlbrowser and sqlite
    By janus in forum Installation and Deployment
    Replies: 2
    Last Post: 31st March 2008, 14: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
  •  
Qt is a trademark of The Qt Company.