sqlite3 puzzle

llanitedave llanitedave at veawb.coop
Tue Jan 15 16:51:33 CET 2013


On Tuesday, January 15, 2013 6:36:51 AM UTC-8, Rob Day wrote:
> On 15 January 2013 07:09, llanitedave <llanitedave at veawb.coop> wrote:
> 
> 
> 
> > 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.
> 
> 
> 
> Hmm - your code doesn't quite match up with the docs at
> 
> http://docs.python.org/2/library/sqlite3.html. That seems to suggest
> 
> that you should call fetchone() on the cursor, not on the result of
> 
> execute().
> 
> 
> 
> Does the following work?
> 
> 
> 
>     # open database file
> 
>     self.geologger_db = sqlite3.connect('geologger.mgc')
> 
>     self.db_cursor = self.geologger_db.cursor()
> 
>     self.db_cursor.execute("PRAGMA foreign_keys = ON")
> 
>     print self.db_cursor.fetchone()
> 
> 
> 
> 
> 
> --
> 
> Robert K. Day
> 
> robert.day at merton.oxon.org

Thanks for the suggestion, Rob, but that didn't make any difference.  I've never had an issue with putting the execute object into a variable and calling "fetch" on that variable.  

I can accept reality if it turns out that foreign keys simply isn't enabled on the Python distribution of sqlite, although I don't know why that should be the case.  I'm just curious as to why it worked at first and then stopped working.



More information about the Python-list mailing list