Ich versuche, Ergebnisse aus einer SQlite-Tabelle abzurufen und sie dann den Variablen in Objective-C zuzuordnen. Meine Methode, die das sieht, sieht so aus:Wie kann man Ergebnisse aus einer SQLite-Tabelle abrufen und sie Variablen in Objective-C zuweisen?
- (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);
}
Ich habe kein Problem beim Abrufen von Zeichenfolgen aus der SQLite-Tabelle. Meine Verwirrung ist, wie man die Breiten- und Längengradvariablen von der SQLite Tabelle erhält, die als Doppeltes gespeichert werden? Wie würde ich diese Werte den Variablen double aLat und double aLon in meinem obigen Code zuweisen?
Das ist eine höllisch lange 'init' Methode, die Sie dort bekommen haben, wie wäre es mit einem' NSDictionary'? –
Verwenden Sie [FMDB] (http://github.com/ccgus/fmdb). –