2010-12-13 4 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' 메소드가 있는데, NSDictionary 만 전달하면 될까요? –

+0

[FMDB] (http://github.com/ccgus/fmdb)를 사용하십시오. –

답변

3

sqlite_column_double을 사용하십시오. 그렇게 단순 :

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

답장을 보내 주셔서 대단히 감사드립니다! – syedfa

0

내가 플로트와의 NSNumber를 만들고 그것의 두 배 값을 받고 생각할 수있는 가장 쉬운 :

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

'aDouble = aFloat;'할 수 있다는 것을 알았습니까? – JeremyP

+0

그걸 알지 못했습니다, 감사합니다! – Rog

+0

시간 내 주셔서 감사합니다. – syedfa

0

다른 필드에했던 것처럼. 그것을 검색하고 double 값을 문자열로 스트로크 할 수 있습니다. 그리고 사전을 만들 것을 제안합니다. 모든 데이터를 사전에 저장할 수 있습니다. 그리고 나중에 Objective C 코드에서 액세스 할 수 있습니다.

+0

시간 내 주셔서 대단히 감사합니다. – syedfa

관련 문제