title: Using foreign keys in sqlite3 for Python
body: <p>I'm writing a program that creates a sqlite3 database through python. I have one table of Authors (AuthorID, Name) and a second table of books (BookID, Title, AuthorID) I've created these as shown below:</p>
  
  <pre><code>Authors = sqlite3.connect('Authors.db')
  Authors.execute('''CREATE TABLE Authors
         (AuthorID      INT  PRIMARY KEY, 
         Name           TEXT);''')
  Authors.close()
  
  
  Books = sqlite3.connect('Books.db')
  Books.execute('''CREATE TABLE Books
         (BookID            INT  PRIMARY KEY, 
         Title              TEXT,
         AuthorID           INT,
         FOREIGN KEY(AuthorID) REFERENCES Authors(AuthorID));''')
  Books.close()
  </code></pre>
  
  <p>I then go to add a record to each of the tables as shown below:</p>
  
  <pre><code>Authors = sqlite3.connect('Authors.db') 
  Authors.execute("INSERT INTO Authors (AuthorID, Name) \
          VALUES (1, 'Jane Austin')");
  
  Authors.commit()
  Authors.close()
  
  
  Books = sqlite3.connect('Books.db')
  Books.execute("INSERT INTO Books (BookID, Title, AuthorID) \
          VALUES (1, 'Emma', 1)");
  
  Books.commit()
  Books.close()
  </code></pre>
  
  <p>The database is correctly updated but I don't think the foreign keys are working correctly because it allows me to remove the Author 'Jane Austin', when there are books associated with it.</p>
  
  <p>I've seen some tutorials use this line: </p>
  
  <pre><code>Books.execute("PRAGMA foreign_keys = 1")
  </code></pre>
  
  <p>Is this the answer to the problem and if so where do I put this line?</p>