-3
我想更新我的表。此代码在模拟器上工作,但不能在设备上工作。我发现我不能写入我的应用程序包中的任何文件,但我没有如何改变它的工作。请帮帮我。Sqlite更新在模拟器上工作,但不能在设备上工作
-(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);
}
唉唉。我之前有过这个问题。复制你的test.sqlite数据库。将其重命名为Test2.sqlite,并将其复制回来。做一个干净的。更新你的源代码成为test2.sqlite并重新运行它。听起来很棘手,但这对我来说很有用。 – ApolloSoftware
你不知何故发现或多或少的正确的代码来设置你的数据库,从捆绑复制到可写空间。您需要跟踪该代码的执行情况,找出发生了什么问题。 –
设备区分大小写。如果你的文件真的命名为“Test.sqlite”而不是“test.sqlite”(或其他一些变体)? – rmaddy