Today I noticed that Foreign key constraint on my SQLite table does not work. After reading on Stack Overflow, I found out that this should be enabled. So, I was looking for code snippet for doing that. So far, I could only find this:
[self.db executeUpdate:@"PRAGMA foreign_keys=ON"];
But this does not seem to work for me, as compiler always complains. I saw people use this line for FMDatabase type (I don't even know what is it).
So, how do I enable foreign key constraint, if I open database connection like this:
- (void) openDatabase
{
const char* databaseFile = [[self pathToDatabaseFile:@"readlater.sql"] UTF8String];
sqlite3 *connection;
if (sqlite3_open(databaseFile, &connection) != SQLITE_OK) {
return;
}
self.db = connection;
}
Or should it be done while creating tables? Thank you.
FMDatabaseclass is part of the FMDB library, a thin Objective-C wrapper around the SQLite C API. Using FMDB can greatly simplify your SQLite code.