-3

我想更新我的表。此代码适用于模拟器,但不适用于设备。我发现我无法写入我的应用程序包中的任何文件,但我不知道如何改变它来工作。请帮我。

-(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);     
    }
4

1 回答 1

0

我认为您在模拟器中进行了更改,但设备上的新数据库尚未更新。

只需从设备中删除您的应用程序并重新运行它,即

清洁安装

于 2013-07-15T16:40:03.347 回答