Enabling foreign_keys = ON in sqlite3 with code

I added a link to several tables one-one, one-many ...

I created a database and tables programmatically, I want to include foreign_keys for my tables, for which I shoot sqlite3_excec(db,PRAGMA foreign_keys=ON,0,0,&error)

which does not work! it creates a text file instead of sqlite3.

please let me know if any solution is included PRAGMA foreign_keys=ON;in sqlite3

+4
source share
1 answer

write this code in the method where you open the database

sqlite3_stmt *enableForeignKey;
NSString *strsql = [NSString stringWithFormat:@"PRAGMA foreign_keys = ON"];

const char *sql=(char *)[strsql UTF8String];
if (sqlite3_prepare_v2(database, sql,-1, &enableForeignKey, NULL) != SQLITE_OK) {
    NSLog(@"ERROR IN PRAGMA!");
}
+2
source

All Articles