我在我的表中有外键约束但是当我使用终端插入数据时它工作正常但是当我在xcode项目中执行它时它不起作用 可能是什么问题。 如何通过程序启用pragma foreign_keys = ON。 我使用下面的代码插入,但我认为外键未启用。 告诉我如何通过程序启用外键。
+(BOOL)insertEmployee:(int)din :(NSString*)name{
NSArray *arrDocPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strDestPath = [NSString stringWithFormat:@"%@/samplepillbox1.sqlite",[arrDocPath objectAtIndex:0]];
sqlite3 *dbEmployee;
if(sqlite3_open([strDestPath UTF8String], &dbEmployee)==SQLITE_OK)
{
//NSString *insertQuery = [NSString stringWithFormat:@"insert into maintable_master(din,brand_name) values(%d,'%@')",din,name];
//NSString *insertQuery = [NSString stringWithFormat:@"insert into doctor_master(din,firstname) values(%d,'%@')",din,name];
NSString *insertQuery = [NSString stringWithFormat:@"insert into schedule_master(din,start_date) values(%d,'%@')",din,name];
void *v;
char *errmsg;
if(sqlite3_exec(dbEmployee, [insertQuery UTF8String], 0, v, &errmsg)==SQLITE_OK)
{
NSLog(@"inserted successfully");
return YES;
}
}
return NO;}
答案 0 :(得分:4)
您应该能够像运行更标准的SQL语句一样设置pragma。通常,您在打开数据库连接后立即设置pragma。
所以你的代码应该看起来像这样:
// 1. open the connection
...
// 2. set the pragma
if (sqlite3_exec(dbEmployee, @"PRAGMA foreign_keys = ON", 0, v, &errmsg) != SQLITE_OK)
{
NSLog(@"failed to set the foreign_key pragma");
return -1;
}