2009-07-10 8 views
2

In dem folgenden Code, ich bin eine Verbindung zu einer SQLite-Datenbank, die SELECT-Abfrage hat nicht funktioniert.iphone SQLite Wählen Abfrage funktioniert nicht

Ich hoffe, Sie können mir helfen.

Dank

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]; 
} 
+0

Wie es "nicht funktioniert"? Ausnahme? Falsches Ergebnis? etwas anderes? – oxigen

Antwort

3

Diese

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

falsche

ist, muss

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

sei es richtig sein könnte, je nachdem, welche Spalte Sie erhalten möchten. In diesem Beispiel denke ich, dass die Tabelle der Datenbank 3 Spalten enthält: id, aName und aScore. Das Problem gibt es nicht, aber hier:

if(sqlite3_step(compiledStatement) == SQLITE_ROW) {

Stellen Sie sicher, dass Ihre compiledStatement einen guten Wert zurückgibt.

0

sollten Sie Pfad wie folgt erhalten;

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