NSString *cityInput = cityField.text;
NSString *code = @"";
NSString *query = @"SELECT code FROM country WHERE cityname = UPPER(?)";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [query UTF8String],-1, &statement, nil) == SQLITE_OK)
{
sqlite3_bind_text(statement, 1, [cityInput UTF8String], -1, SQLITE_STATIC);
while(sqlite3_step(statement) == SQLITE_ROW) {
code = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)];
}
}
if(sqlite3_step(statement) != SQLITE_DONE){
NSLog(@"DB: query KO");
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"ALERT" message:@"City not found" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alertView show];
return;
}
sqlite3_close(database);
两个问题:
1)获取单行我必须使用while循环吗?
2)如果查询中没有结果警告“找不到城市”
答案 0 :(得分:1)
显然你会得到最后一行,
code
应该是一个数组,你可能会像这样声明它
NSMutableArray *code=[NSMutableArray array];
然后在while循环中使用
[code addobject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)]];
我希望这很清楚:)
答案 1 :(得分:0)
答案 2 :(得分:0)
也许有两个答案,
1)不,只需要一个if子句
2)做一个assignement然后在它上面运行一个if子句
int result = sqlite3_step(statement)
if (result == SQLITE_ROW){
//processline
}
else{
//show alert
}