2010-12-13 7 views
0

私はSQliteテーブルから結果を取得し、Objective-Cの変数に割り当てようとしています。SQLiteテーブルから結果を取得し、それをObjective-Cの変数に割り当てる方法は?

- (void) readRestaurantsFromDatabase { 

//Setup the database object 
sqlite3 *database; 

//Init the restaurant array 
restaurants = [[NSMutableArray alloc] init]; 

//Open the database from the users filesystem 
if (sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 

    //setup the SQL statement and compile it for faster access 
    const char *sqlStatement = "select * from restaurant"; 
    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) { 

      //read the data from the result row 
      NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
      NSString *aAddress = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 2)]; 
      NSString *aCity = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 3)]; 
      NSString *aProvinceState = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 4)]; 
      NSString *aPostalZipCode = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 5)]; 
      NSString *aCountry = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 6)]; 
      NSString *aPhoneNumber = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 7)]; 
      NSString *aHours = [NSString stringWithUTF8String:(char *)sqlite_coluumn_text(compiledStatement, 8)]; 
      //double aLat 
      //double aLon 

      Restaurant *restaurant = [[Restaurant alloc] initWithName:aName address:aAddress city:aCity provinceState:aProvinceState postalZipCode:aPostalZipCode country:aCountry phoneNumber:aPhoneNumber hours:aHours latitude:aLat longitude:aLon]; 

      //add the restaurant object to the restaurant array 
      [restaurants addObject:restaurant]; 

      [restaurant release]; 

     } 


    } 

    sqlite3_finalize(compiledStatement); 

} 

sqlite3_close(database); 

} 

sqliteテーブルから文字列を取得する際に問題はありません。私の混乱は、倍精度として格納されているSQLiteテーブルから緯度と経度の変数を取得する方法ですか?私はどのようにこれらの値をdouble aLat変数に代入し、上のコードでdouble aLonを代入するのですか?

+0

これはあなたがそこにある長い 'init'メソッドの1つの地獄です。ちょうど' NSDictionary'を渡すのはどうですか? –

+0

[FMDB](http://github.com/ccgus/fmdb)を使用してください。 –

答えて

3

sqlite_column_doubleを使用してください。あなたが他のフィールドのためにやっていたよう

NSNumber *number = [NSNumber numberWithFloat:aFloat]; 
double aDouble = [number doubleValue]; 
+0

お返事ありがとうございました! – syedfa

0

最も簡単。これを取得してdouble値をストリングとしてストロークすることができます。また、辞書を作成することをお勧めします。すべてのデータを辞書に保存することができます。その後、Objective Cコードでアクセスすることができます。

+0

あなたは 'aDouble = aFloat;'を実行できることを知っていますか? – JeremyP

+0

それを知りませんでした、ありがとう! – Rog

+0

私の質問に答える時間をとってくれてありがとう! – syedfa

0

:そのような単純な:私はフロートとのNSNumberを作成し、それからdouble値を取得していると考えることができます

double aLon = sqlite_column_double(compiledStatement, 9); 
double aLat = sqlite_column_double(compiledStatement, 10); 
+0

私の質問に答える時間をとってくれてありがとう! – syedfa

関連する問題