【问题标题】:iphone SQLite Select Query doesn't workiphone SQLite 选择查询不起作用
【发布时间】:2010-11-09 15:07:03
【问题描述】:

在下面的代码中,我连接到一个 SQLite 数据库,SELECT 查询不起作用。

我希望你能帮助我。

谢谢

if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
    // Setup the SQL Statement and compile it for faster access
    const char *sqlStatement = "select name,score from game Where name='interclock'";
    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, 1)];
            NSString *aScore =[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)];

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Sonuç" message:[NSString stringWithFormat:@"Oyun adı %s Skor:%s",aName,aScore] 
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];   

            //NSString *aName = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 2)];
            //NSString *aScore = [NSString stringWithString:(NSString *)sqlite3_column_text(compiledStatement, 3)];



            // Create a new animal object with the data from the database
            DatabaseClass *dbOBJ = [[DatabaseClass alloc] initWithName:aName score:aScore];

            // Add the animal object to the animals Array
            [scores addObject:dbOBJ];

            [dbOBJ release];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"SQL Query Dont Work" 
                                                           delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
            [alert show];
        }
    }
    // Release the compiled statement from memory
    sqlite3_finalize(compiledStatement);

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

【问题讨论】:

  • 它如何“不工作”?例外?结果不正确?还有什么?

标签: iphone objective-c xcode sqlite


【解决方案1】:

这是错误的

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

必须

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

【讨论】:

    【解决方案2】:

    你应该得到这样的路径;

    NSString *path = [[NSBundle mainBundle]pathForResource:@"sarkiSozleri"ofType:@"sqlite"];
    

    【讨论】:

      【解决方案3】:

      这可能是正确的,具体取决于您想要获取的列。在这个例子中,我认为数据库的表包含 3 列:id、aName 和 aScore。问题不在那里,但在这里:

      if(sqlite3_step(compiledStatement) == SQLITE_ROW) {

      验证您的 compiledStatement 是否返回了合适的值。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多