0

我正在尝试在应用程序 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];


 }
4

2 回答 2

2

我认为问题出在您的插入查询代码中

NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@',)",11,@"auto", @"YES", @"YES", @"HIGH"];

您又添加了一个,最后在 values() 检查并删除最后一个,它肯定会起作用

NSString *query = [NSString stringWithFormat:@"insert into settings(id,defaultaction,beepsound,vibrateeffect,level) values (%d, '%@', '%@','%@','%@')",11,@"auto", @"YES", @"YES", @"HIGH"];

试试这个查询

于 2013-02-21T11:55:30.713 回答
1
NSString *query = @"INSERT into settings(id,defaultaction,beepsound,vibrateeffect,level) 
VALUES (%@, %@, %@,%@,%@)";

BOOL y = [database executeUpdate:query, [NSNumber numberWithInt:11],@"auto", @"YES", @"YES", @"HIGH"];

尝试将您的查询更改为上述查询,如果表创建成功,它将确定工作。

祝你好运。

于 2013-02-22T07:06:31.677 回答