今天,我注意到我的SQLite表上的外键约束不起作用。阅读了Stack Overflow之后,我发现应该启用它。因此,我一直在寻找这样做的代码片段。到目前为止,我只能找到以下内容:
[self.db executeUpdate:@"PRAGMA foreign_keys=ON"];
但这似乎对我不起作用,因为编译器总是抱怨。我看到人们用这行作为FMDatabase
类型(我什至不知道它是什么)。因此,如果打开像这样的数据库连接,如何启用外键约束:
- (void) openDatabase
{
const char* databaseFile = [[self pathToDatabaseFile:@"readlater.sql"] UTF8String];
sqlite3 *connection;
if (sqlite3_open(databaseFile, &connection) != SQLITE_OK) {
return;
}
self.db = connection;
}
还是应该在创建表时完成?谢谢。
直接使用SQLite C API时,还必须使用C函数来执行SQL命令:
sqlite3_exec(connection, "PRAGMA foreign_keys = on", NULL, NULL, NULL);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句