1

下面是我更新数据库记录的代码...

-(BOOL) addScoreInDatabase:(classname *)objGames
{
    BOOL success;
    sqlite3 *database;

    const char *dbpath = [self.databasePath UTF8String];

    if(sqlite3_open(dbpath, &database) == SQLITE_OK)
    {
        sqlite3_stmt *statement;

        NSData *strJson = [objGames getJSON];

        NSString *strContent = [[[[NSString alloc]
                                  initWithData:strJson encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\\" withString:@""]stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
        NSLog(@"strcom %@",strContent);
        NSString *query = [NSString stringWithFormat:@"UPDATE gamescore SET data ='%@' WHERE id = 1",strContent];
        NSLog(@"query %@",query); 
        const char *sqlStatement = [query UTF8String];
        if(sqlite3_prepare_v2(database, sqlStatement, -1, & statement, NULL) == SQLITE_OK)
        {
            if (sqlite3_step(statement) == SQLITE_DONE)
            {
                success = TRUE;
            }
            else
            {
                success = FALSE;
            }
            sqlite3_exec(database, "VACUUM;", 0, 0, nil);
        }
        sqlite3_finalize(statement);
        sqlite3_close(database);
    }
    return success;
}

它不会进入if (sqlite3_step(statement) == SQLITE_DONE) 循环

结果查询是

UPDATE gamescore SET data ='{
  "title" : "pacman",
  "time" : "55",
  "score" : "200",
  "level" : "1"
}' WHERE id = 1

当我在 Sqlite Manager 中执行此查询时,它不会更新记录,而是显示类似这样的内容在此处输入图像描述

谁能帮我更新我的记录?!..谢谢!

4

1 回答 1

0

您可以使用 sqlite3_exec() 方法代替 sqlite3_step()。

sqlite3_exec() 将执行您给出的任何查询。

试试这个。

enter code 
-(BOOL) addScoreInDatabase:(classname *)objGames
{
BOOL success;
sqlite3 *database;

const char *dbpath = [self.databasePath UTF8String];

if(sqlite3_open(dbpath, &database) == SQLITE_OK)
{
    sqlite3_stmt *statement;

    NSData *strJson = [objGames getJSON];

    NSString *strContent = [[[[NSString alloc]
                              initWithData:strJson encoding:NSUTF8StringEncoding] stringByReplacingOccurrencesOfString:@"\\" withString:@""]stringByReplacingOccurrencesOfString:@"null" withString:@"\"\""];
    NSLog(@"strcom %@",strContent);
    NSString *query = [NSString stringWithFormat:@"UPDATE gamescore SET data ='%@' WHERE id = 1",strContent];
    NSLog(@"query %@",query); 
    const char *sqlStatement = [query UTF8String];
    if(sqlite3_prepare_v2(database, sqlStatement, -1, & statement, NULL) == SQLITE_OK)
    {
        sqlite3_exec(database, "VACUUM;", 0, 0, nil);
    }
    sqlite3_finalize(statement);
    sqlite3_close(database);
}
return success;

}

于 2013-04-03T07:22:38.157 回答