我正在尝试在应用程序 didFinishLaunchingWithOptions 的 AppDelegate 中的数据库中插入一些值,但是每次插入数据失败时。
代码
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *path = [docsPath stringByAppendingPathComponent:@"db.sqlite"];
FMDatabase *database = [FMDatabase databaseWithPath:path];
[database open];
FMResultSet *results = [database executeQuery:@"select * from settings"];
if(results == nil)
{
NSLog(@"Creating DB");
[database beginTransaction];
[database executeUpdate:@"create table settings(id int primary key, defaultaction text, beepsound text, vibrateeffect text, level text)"];
NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@',)",11,@"auto", @"YES", @"YES", @"HIGH"];
NSLog(@" %@",path);
BOOL y= [database executeUpdate:query];
if (!y)
{
NSLog(@"insert failed!!");
}
[database commit];
[database close];
}