开发者

How to retrieve results from a SQLite table and assigning it to variables in Objective-C?

I am trying to retrieve results from a SQlite table, and then assign them to variables in Objective-C. My method that does this looks like this:

- (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";
    s开发者_如何学Goqlite3_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);

}

I don't have a problem retrieving strings from the sqlite table. My confusion is how to retrieve the latitude, and longitude variables from the SQLite table which are stored as doubles? How would I assign these values to the variables double aLat, and double aLon in my code above?


Use sqlite_column_double. Simple as that:

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


Easiest I can think of is creating a NSNumber with the float and getting the double value out of it:

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


As you have did for other fields. You can retrieve it and strore the double value as a string. And i also suggest you to create a dictionary. You can store all your data in dictionary. And afterward you can access it in your Objective C code.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜