Skip to main content

Thread: sqlite3: turning on foreign key support thru python


i'm setting sqlite3 database use base programming stuff want work on. using python 2.7, appears have new enough version of sqlite (just barely) support foreign keys.

understand things, sqlite default has foreign keys turned off, unless compiled otherwise or until turn on foreign keys using 'pragma foreign_keys=on'. , needs turned on each connection too.

so... putzing around using python interactive shell...

code:
>>>import sqlite3  >>>sqlite3.sqlite_version  '3.6.21'  >>>conn = sqlite3.connect('contacts.db')  >>>conn.execute('pragma foreign_keys=on')  <sqlite3.cursor object @ 0x00b61860>  >>>conn.execute('pragma foreign_keys')  <sqlite3.cursor object @ 0x00b6f020>  >>>
it appears able import sqlite3, of recent enough version support foreign keys (> 3.6.19), connected existing database 'contacts.db', , when execute pragma statement turn on foreign key support returns cursor object. similarly, when send pragma statement query status of foreign key support, returns cursor object.

stupid question(s):

how tell if succeeded? how use cursor object returned pragma query tell if '1' (on) or '0' (off)?

got answer source; figured i'd pass on here benefit of using search engine in future

need iterate on cursor object returned pragma statement so:

code:
rows = conn.execute('pragma foreign_keys')  row in rows:      print row
which should return tuple of (1,) or (0,) depending on whether foreign key support enabled or not.

fwiw, statement turn on foreign keys:

code:
conn.execute('pragma foreign_keys=on')
also returns cursor object, 1 *not* iterable.


Forum The Ubuntu Forum Community Ubuntu Specialised Support Development & Programming Programming Talk sqlite3: turning on foreign key support thru python


Ubuntu

Comments

Popular posts from this blog

Falang and too many redirects - Joomla! Forum - community, help and support

Infinite loop detected in JErrorInfinite loop detected in JError - Joomla! Forum - community, help and support

logged out from joomla! - Joomla! Forum - community, help and support