2011-12-14 3 views
0

これで、SQLiteクエリでうまくいくようになっていますが、明らかにまだマスターではありません。iOS 5のすべてのエントリに対して0を返すSQLite3クエリ

これが返す配列をループしようとしていますが、すべてのエントリに対して0が得られます。今ここに呼び出し、それをループするときに私がやっているものです

-(NSMutableArray *)getMyAthleteList 
{ 
NSMutableArray *athleteList = [[NSMutableArray alloc] init]; 

NSString *getAthleteListSQL = [[NSString alloc] initWithFormat:@"SELECT * FROM athletes"]; 

sqlite3_stmt *selectStmt; 

if (sqlite3_prepare_v2(database, [getAthleteListSQL UTF8String], -1, &selectStmt, nil)==SQLITE_OK) 
{ 
    while (sqlite3_step(selectStmt)==SQLITE_ROW) 
    { 
     //NSLog(@"Adding athletes to the array"); 
     Athletes *athlete_array = [Athletes alloc]; 
     athlete_array.athlete_image = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 0)]; 
     athlete_array.athlete_id = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 1)]; 
     athlete_array.athlete_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 2)]; 
     athlete_array.school_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 3)]; 
     athlete_array.sport_name = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 4)]; 
     athlete_array.athlete_flag = [[NSString alloc] initWithFormat:@"%d",sqlite3_column_int(selectStmt, 5)]; 
     [athleteList addObject:athlete_array]; 
    } 
} 
else 
{ 
    NSLog(@"Did not make a good query"); 
} 

return athleteList; 
} 

ここ

問い合わせ電話をかけると、結果を返しているものですが(この特定のクエリは、現在、データの6行を返す必要があります):

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate]; 

NSArray *myAthleteList = [[NSArray alloc] initWithArray:[appDelegate getMyAthleteList]]; 

for (Athletes *athlete in myAthleteList) { 
    NSLog(@"Athlete Name: %@", athlete.athlete_name); 
    NSLog(@"Athlete ID: %@", athlete.athlete_id); 
} 

、出力は次のとおりです。

選手名:0 アスリートID:0

私の文字列(athlete_name、athlete_idなど)はすべて正しく定義されています(Athletes.mおよび.hファイル内)。

私は何かが簡単ではないと確信していますが、どんな助けも高く評価されます。

ありがとうございます!

+0

マソチストはObjective-Cで直接SQLite C APIを使用します。 [FMDBの使用](http://github.com/ccgus/fmdb)(SQLiteラッパー)または[CoreData](http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/CoreData/ cdProgrammingGuide.html)(オブジェクトグラフマネージャ)を使用します。 –

+0

うわー... FMDBはずっと簡単です。ありがとう! – TheTC

答えて

1

DBから文字列を返すような行がありますか?もしそうなら

athlete_array.athlete_name = [[NSString alloc] initWithFormat:@"%d", 
           sqlite3_column_int(selectStmt, 2)]; 

、あなたはsqlite3_column_textに見たいと思うかもしれませんかtext16

参照:

[[NSString alloc] initWithUTF8String:(const char *) sqlite3_column_text(...) 
http://www.sqlite.org/capi3ref.html#sqlite3_column_blob

それはのconstのchar *を返すので、また、これを使用することもできます

たとえば、私のサンプルの1つと同様のスニペットがあります。

contact = [[Contact alloc] init]; 
while (sqlite3_step(statement) == SQLITE_ROW) 
{ 
    int idx = 0; 

    NSNumber *idField = [NSNumber numberWithLongLong:sqlite3_column_int64(statement, idx++)]; 
    [contact setId:idField]; 

    NSString *nameField = [[NSString alloc] initWithUTF8String: 
          (const char *) sqlite3_column_text(statement, idx++)]; 
    [contact setName:nameField]; 

    NSString *addressField = [[NSString alloc] initWithUTF8String: 
          (const char *) sqlite3_column_text(statement, idx++)]; 
    [contact setAddress:addressField]; 

    NSString *phoneField = [[NSString alloc] initWithUTF8String: 
          (const char *) sqlite3_column_text(statement, idx)]; 
    [contact setPhone:phoneField]; 

    NSLog(@"id: %@", [contact id]); 
    NSLog(@"name: %@", [contact name]);    
    NSLog(@"address: %@", [contact address]); 
    NSLog(@"phone: %@", [contact phone]); 

    // Code to do something with extracted data here 

    [nameField release]; 
    [phoneField release]; 
    [addressField release]; 
} 

さらに、コンパイルされたステートメント&のselectStmtを保存し、sqlite3_resetを呼び出して再度使用する方法を調べてください。その時点でバイトコードにコンパイルされ、SQL文を何度も解析してコンパイルする必要はありません。

を参照してください:あなたは(あなたがそれを保存し、完全な場合)文で終わったらhttp://www.sqlite.org/capi3ref.html#sqlite3_prepare

、sqlite3_finalizeを呼び出すことを忘れないでください。

関連する問題