3

I'm using this to do an Update statement in a test iOS App, the log outputs 'Success' but theres no change in the database. Could anyone point me in the right direction? It'd be greatly appreciated.

I'm brand new to this so please forgive my probably appallingly written code.

@try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"chars.sqlite3"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }
    const char *sql = "UPDATE characters SET level = 'testing' WHERE id='1'";
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{
        if (sqlite3_step(sqlStatement) == SQLITE_DONE){
            NSLog(@"Success");
        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);
}
4

1 回答 1

4

问题是您正在从包中打开数据库,它是只读的。你应该

  1. 检查 Documents 文件夹中是否存在数据库。

  2. 如果不存在,请将数据库从捆绑包复制到 Documents 文件夹。

  3. 现在,从 Documents 文件夹中打开数据库。

于 2013-08-11T13:49:44.980 回答