我想更新我的表。此代码适用于模拟器,但不适用于设备。我发现我无法写入我的应用程序包中的任何文件,但我不知道如何改变它来工作。请帮我。
-(NSString *)filePath
{
NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir=[paths objectAtIndex:0];
NSError *error;
bool success;
NSString *writablePath = [documentsDir stringByAppendingPathComponent:@"Test.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:writablePath];
if(!success)
{
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Test.sqlite"];
success = [fileManager copyItemAtPath:defaultPath toPath:writablePath error:&error];
}
NSLog(@"%@",writablePath);
return writablePath;
}
接着:
- (IBAction)addToFavorite:(id)sender
{
[self filePath];
sqlite3_stmt *updateStatement=nil;
NSString *sql=[[NSString alloc]initWithFormat:@"UPDATE A SET favorite=? WHERE x='%@' ",mystring];
if (sqlite3_prepare_v2(db, [sql UTF8String], -1, &updateStatement, nil)==SQLITE_OK)
{
sqlite3_bind_text(updateStatement, 1, [@"1" UTF8String], -1, SQLITE_TRANSIENT);
}
if (sqlite3_step(updateStatement)!=SQLITE_DONE)
{
NSAssert(0,@"Error While Creating Update Statement. '%s'",sqlite3_errmsg(db));
}
sqlite3_reset(updateStatement);
}