我有一个DBManager
从数据库(sqlite 文件)中获取数据的方法。所有其他查询都很好,但这一个似乎无法正常工作
-(NSArray *)readCountries{
NSLog(@"[DBManager] readCountries");
NSMutableArray *countriesArray = [[NSMutableArray alloc] init];
//open db from users filesystem
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char* sql = "SELECT DISTINCT country FROM aed ORDER BY rowid";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {
//loop through results
while (sqlite3_step(statement) == SQLITE_ROW) {
//read data from record
NSString *_country;
char* tmpCountry = (char*)sqlite3_column_text(statement, 1);
NSLog(@"tmpCountry = %@", [NSString stringWithUTF8String:tmpCountry]);
if (tmpCountry != NULL) {
_country = [NSString stringWithUTF8String:tmpCountry];
}else{
_country = @"n/a";
}
NSLog(@"country = %@", _country);
[countriesArray addObject:_country];
}
}
//finalize statement
sqlite3_finalize(statement);
}
//close database
sqlite3_close(database);
NSLog(@"[DBManager] countriesArray has %d objects", [countriesArray count]);
return (NSArray*)countriesArray;
}
我从日志中得到的所有信息,我的数组有 5 个对象,这很好 - 但它不仅仅是“n/a”......任何想法?其他查询很好,他们主要使用sqlite3_column_text
所以我不明白,为什么它在这里不起作用 - 也许新的眼睛会有所帮助。