iphone - 桌面上的SQLite记录不会出现?

问题描述 投票:0回答:3

我有一张桌子和3条记录。

我有代码;

-(void) readScoreFromDatabase {

sqlite3 *database;


scores = [[NSMutableArray alloc] init];


if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

    const char *sqlStatement = "select name,score from game";
    sqlite3_stmt *compiledStatement;
    if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {



        // Loop through the results and add them to the feeds array
        while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
        //if(sqlite3_step(compiledStatement) == SQLITE_ROW) {
            // Read the data from the result row

            NSString *aName =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)];
            NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];


            DatabaseClass *dbOBJ = [[DatabaseClass alloc] initWithName:aName score:aScore];


            [scores addObject:dbOBJ];

            [dbOBJ release];
        } 
    }

    sqlite3_finalize(compiledStatement);

} else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No Connection" 
                                                   delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
}
sqlite3_close(database);

}

我正在使用该代码来显示记录;

-(IBAction)refreshClick:(id)sender {

// Navigation logic -- create and push a new view controller
IDRGameAppDelegate *appDelegate = (IDRGameAppDelegate *)[[UIApplication sharedApplication] delegate];
DatabaseClass *dbOBJ = (DatabaseClass *)[appDelegate.scores objectAtIndex:1];

game1NameLabel.text = dbOBJ.name;
score1Label.text = dbOBJ.score;
}

我有3条记录,但我只能记录一条记录。我的意思是更改了那行“DatabaseClass * dbOBJ =(DatabaseClass *)[appDelegate.scores objectAtIndex:1];” objectAtIndex:1当我更改此值1或2或3等时,结果不会更改。它始终显示3条记录中的一条记录。我不明白原因。

谢谢。

iphone database sqlite select
3个回答
1
投票

当您转到SQLite管理器(来自Firefox)并运行以下查询时,您是否能够看到表行:

select name,score from game;

如果您能够看到3条记录,那么:

  • 转到iPhone模拟器并重置设置(将清除所有临时缓存,数据库和构建文件)。
  • 转到Xcode并执行Build和GO。

这将为您提供最新的数据库;把它放在Build文件夹中。


0
投票

这可能不是您想要的答案,但您现在应该考虑使用Core Data,因为它在iPhone OS 3.0中。它可以为您处理大部分内容,并且非常易于使用。


0
投票

也许你忘了为你的代表设置分数?

scores = [[NSMutableArray alloc] init];
....

//I don't see this in your code
((IDRGameAppDelegate *)[[UIApplication sharedApplication] delegate]).score = score;    
© www.soinside.com 2019 - 2024. All rights reserved.