FMDataBase 打开sqlite的外键约束功能
//create databaseNSString* dbPath =[(NSArray*)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
dbPath =[dbPath stringByAppendingPathComponent:@"test.db"];
db =[FMDatabase databaseWithPath:dbPath];if([db open]){NSLog(@"Database %@ opened", dbPath);//check for foreign_keyNSString* sql =@"PRAGMA foreign_keys";FMResultSet*rs =[db executeQuery:sql];int enabled;if([rs next]){
enabled =[rs intForColumnIndex:0];}[rs close];if(!enabled){// enable foreign_key
sql =@"PRAGMA foreign_keys = ON;";[db executeUpdate:sql];// check if successful
sql =@"PRAGMA foreign_keys";FMResultSet*rs =[db executeQuery:sql];if([rs next]){
enabled =[rs intForColumnIndex:0];}[rs close];}// do your stuff here, or just cache the connection}else{NSLog(@"Failed to open %@", dbPath);}
posted on 2013-04-01 10:04 kiao295338444 阅读(294) 评论(0) 编辑 收藏 举报