if(sqlite3_step(statement)== SQLITE_DONE)循环不执行 - iOS

时间:2013-04-03 07:07:31

标签: ios database

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

-(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中执行此查询时,它不会更新记录,而是显示类似enter image description here

的内容

有人可以帮我更新我的记录吗?!..谢谢!!

1 个答案:

答案 0 :(得分: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;

}