我正在尝试在iOS应用程序的SQLite数据库中保存一些数据。我正在使用以下方法进行此操作:
NSString *query = [NSString stringWithFormat:@"INSERT INTO categories VALUES ('%@', '%@', '%@', null, %d)", categoryName, @"test", @"test", 33];
NSLog(@"Query is: %@", query);
[[AFSQLManager sharedManager]performQuery:query withBlock:^(NSArray *row, NSError *error, BOOL finished) {
if (!error) {
if (!finished) {
NSLog(@"No success");
} else {
NSLog(@"Succeeded");
}
} else {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Oops" message:error.description delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];
这是打开数据库的方法,该方法在中执行viewDidLoad
:
[[AFSQLManager sharedManager]openLocalDatabaseWithName:@"test.sqlite" andStatusBlock:^(BOOL success, NSError *error) {
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:@"Oops" message:error.description delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
}
}];
如您所见,我正在使用AFSQLManager来执行查询(您可以在此处找到它https://github.com/AlvaroFranco/AFSQLManager)。上面的方法记录“成功”,但是当我从数据库中提取一些数据时,没有任何结果。我最好的猜测是这是SQLite
数据库的问题。...知道我在做什么错吗?
您引用的类在其打开例程中存在一个相当基本的问题,即它从捆绑软件中打开数据库:
-(void)openLocalDatabaseWithName:(NSString *)name andStatusBlock:(statusBlock)status {
NSString *path = [[NSBundle mainBundle]pathForResource:[[name lastPathComponent]stringByDeletingPathExtension] ofType:[name pathExtension]];
if (sqlite3_open([path UTF8String], &_database) != SQLITE_OK) {
NSLog(@"Failed to open database!");
status(NO, nil);
} else {
NSLog(@"Database opened properly");
status(YES, nil);
}
}
该捆绑软件是只读的。因此,任何更新该数据库中数据的尝试都将失败。
如果要更新数据库,则必须先将数据库复制到“文档”文件夹(或高速缓存或临时文件夹等)(或在其中创建数据)。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句