sqlite3 puzzle

llanitedave llanitedave at veawb.coop
Tue Jan 15 02:09:28 EST 2013


I'm trying to get an application working in Python 2.7 and wx.Python which contains an embedded sqlite3 file.  There are a few tables with foreign keys defined.  In looking at the sqlite3 documentation, it says 

"Assuming the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime, using the PRAGMA foreign_keys command." 
It goes on to say that foreign keys must be enabled separately for each connection, which is fine as my app has only one.

So I put the following test code in my initialization method:

    # open database file
    self.geologger_db = sqlite3.connect('geologger.mgc')
    self.db_cursor = self.geologger_db.cursor()
    self.foreign_key_status = self.db_cursor.execute("PRAGMA foreign_keys = ON")
    self.foreign_key_status = self.foreign_key_status.fetchone()

    print self.foreign_key_status

I ran this several times while I was arranging the downstream queries, and each time it returned '(1,)', which means foreign keys is enabled.

But I was using a variable named 'cmd1' as a placeholder until I changed the name to the more descriptive 'self.foreign_key_status'.  Since I made the name change, however, the code only returns 'None'.  Reverting to the previous variable name has no effect.

Yes, I'm closing the connection with self.db_cursor.close() at the end of each run.

According to the sqlite3 website, getting no return value, not even a '0', means that "the version of SQLite you are using does not support foreign keys (either because it is older than 3.6.19 or because it was compiled with SQLITE_OMIT_FOREIGN_KEY or SQLITE_OMIT_TRIGGER defined)."

How can that be a compilation issue when it worked previously?  Does this somehow relate to it being a Python plugin instance?

I'm very confused.



More information about the Python-list mailing list