Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. SQLite3 Foreign Keys
QtWS25 Last Chance

SQLite3 Foreign Keys

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 3.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on 31 Aug 2018, 00:51 last edited by A Former User 9 Jan 2018, 07:51
    #1

    At the beginning of my program, I have this:

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("myDb");
    
        QSqlQuery query(db);
        query.exec("PRAGMA foreign_keys = ON;");
    }
    

    And I got that from Asimov, here on the Forum. In all the posts on this topic that I've read, they're all fairly consistent in their remedies to the elusive turning on the SQLite3 Foreign Key apparatus. But just a simple delete in the master table fails to delete across the detail tables.

    Master table:

    query1 = "CREATE TABLE IF NOT EXISTS recipes("
                    "recipe_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                    "name VARCHAR(50);";
    

    Detail table:

    query2 = "CREATE TABLE IF NOT EXISTS recipe_ingredients("
                    "ingredient_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                    "ingredient TEXT, "
                    "amount FLOAT, "
                    "inventory_id INTEGER, "
                    "recipe_id INTEGER, "
                    "FOREIGN KEY(recipe_id) REFERENCES recipes(recipe_id) "
                    "ON DELETE CASCADE ON UPDATE CASCADE)";
    

    I've been messing around with this for a while, long before even helping Noah drive nails in the Ark! But I can't get it to fire up.
    And while it's not as elegant, I can hard code all deletes and updates. In all honesty, I don't think turning on foreign keys (as shown in my code) can be useful inside Qt until Qt has data-aware components. I sure wld like for someone to prove me wrong =)

    J 1 Reply Last reply 31 Aug 2018, 03:37
    0
    • ? A Former User
      31 Aug 2018, 00:51

      At the beginning of my program, I have this:

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      
          db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName("myDb");
      
          QSqlQuery query(db);
          query.exec("PRAGMA foreign_keys = ON;");
      }
      

      And I got that from Asimov, here on the Forum. In all the posts on this topic that I've read, they're all fairly consistent in their remedies to the elusive turning on the SQLite3 Foreign Key apparatus. But just a simple delete in the master table fails to delete across the detail tables.

      Master table:

      query1 = "CREATE TABLE IF NOT EXISTS recipes("
                      "recipe_id INTEGER PRIMARY KEY AUTOINCREMENT,"
                      "name VARCHAR(50);";
      

      Detail table:

      query2 = "CREATE TABLE IF NOT EXISTS recipe_ingredients("
                      "ingredient_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                      "ingredient TEXT, "
                      "amount FLOAT, "
                      "inventory_id INTEGER, "
                      "recipe_id INTEGER, "
                      "FOREIGN KEY(recipe_id) REFERENCES recipes(recipe_id) "
                      "ON DELETE CASCADE ON UPDATE CASCADE)";
      

      I've been messing around with this for a while, long before even helping Noah drive nails in the Ark! But I can't get it to fire up.
      And while it's not as elegant, I can hard code all deletes and updates. In all honesty, I don't think turning on foreign keys (as shown in my code) can be useful inside Qt until Qt has data-aware components. I sure wld like for someone to prove me wrong =)

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 31 Aug 2018, 03:37 last edited by
      #2

      @landslyde said in SQLite3 Foreign Keys:

      But just a simple delete in the master table fails to delete across the detail tables.

      Do you mean the entry in recipes gets deleted but the entry/entries in recipe_ingredients remain? Sounds like foreign keys have not been turned on.

      To check: Try to insert something into recipe_ingredients with an invalid recipe_id. The insertion should fail.

      Did you call the pragma before creating the tables?

      I don't think turning on foreign keys (as shown in my code) can be useful inside Qt until Qt has data-aware components.

      Qt doesn't do anything special -- it just forwards all your queries to the underlying SQLite library.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      1 Reply Last reply
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on 1 Sept 2018, 07:50 last edited by A Former User 9 Jan 2018, 19:41
        #3

        The following code works. And you don't have to have SQLite3 installed with foreign_keys = on. You do, however, need to make sure NO query is performed before this code runs. Do that and this works like a charm.

        MainWindow::MainWindow(QWidget *parent) :
            QMainWindow(parent),
            ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        
            db = QSqlDatabase::addDatabase("QSQLITE");
            db.setDatabaseName("myDb");
        
            if (!db.open())
                ui->statusBar->showMessage(db.lastError().text());
            else
            {
                // NOT EVERYONE WILL HAVE FOREIGN KEYS ENABLED.
                // SO THE SQLITE3 DEVS GAVE US THIS LITTLE GEM 
                // TO ENSURE FOREIGN KEY ACCESS.
                QSqlQuery query(db);
                query.exec("PRAGMA foreign_keys = ON;");
        
                ui->statusBar->showMessage("Database connected");
            }
        }
        
        1 Reply Last reply
        5

        1/3

        31 Aug 2018, 00:51

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved