Results 1 to 3 of 3

Thread: Unable to switch the sqlite3 database foreign keys on

  1. #1
    Join Date
    Nov 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Unable to switch the sqlite3 database foreign keys on

    I'v faced strange problem. Executing the "PRAGMA foreign_keys = ON" query on the sqlite3 database has no effect.

    Qt Code:
    1. //...
    2. QSqlQuery query(_database);
    3.  
    4. if (!query.exec("PRAGMA foreign_keys = ON"))
    5. qCritical() << "QSQLQuery::exec";
    6.  
    7. if (!query.exec("PRAGMA foreign_keys"))
    8. qCritical() << "QSQLQuery::exec";
    9.  
    10. if (query.next())
    11. // Always prints "0", while using sqlite3 command line tool
    12. // "PRAGMA foreign_keys" query returns 0 and 1, in correspondence with
    13. // the value, which was set by the last "PRAGMA foreign_keys = ON|OFF" query
    14. qDebug() << query.value(0).toUInt();
    15.  
    16. if (!query.prepare("DELETE FROM \"MASTER_TABLE\" WHERE ID = :id"))
    17. qCritical() << "QSQLQuery::prepare";
    18.  
    19. query.bindValue(":id", id);
    20. if (!query.exec())
    21. qCritical() << "QSQLQuery::exec";
    22.  
    23. // MASTER_TABLE record is deleted, DETAIL_TABLE, containing foreign key
    24. // with "on delete cascade" option not deleted
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Nov 2013
    Posts
    5
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Unable to switch the sqlite3 database foreign keys on

    Problem was in that query of changing foreign keys control policy was executed inside a transaction.
    As sqlite docs say:
    It is not possible to enable or disable foreign key constraints in the middle of a multi-statement transaction (when SQLite is not in autocommit mode). Attempting to do so does not return an error; it simply has no effect.
    Solved, sorry for disturbance.

  3. #3
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Unable to switch the sqlite3 database foreign keys on

    Thanks for posting the solution, I wasn't aware of that restriction!
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

Similar Threads

  1. Unable to work with database
    By Atomic_Sheep in forum Newbie
    Replies: 9
    Last Post: 18th August 2013, 18:17
  2. Get primary and foreign keys from tables
    By NoRulez in forum Qt Programming
    Replies: 2
    Last Post: 5th November 2008, 11:16
  3. How to get list of database's table's foreign keys?
    By jambrek in forum Qt Programming
    Replies: 3
    Last Post: 26th September 2008, 05:50
  4. How can I save a QImage object into a SQLite3 database table?
    By danielperaza in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2008, 05:39
  5. Nullable foreign keys in QTableView
    By Banjo in forum Newbie
    Replies: 3
    Last Post: 31st January 2008, 22:07

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.