条件付きselectの結果を複数行扱う場合はこのような感じになります。
sqlite3_bind_intのindexが1から始まるのが気持ち悪い・・・。
sqlite3 *database = nil;
int result = sqlite3_open([path fileSystemRepresentation], &database);
if (result != SQLITE_OK) {
return NO;
}
int id_min = 100;
const char *sql = "select id, name from tb1 where id > ?";
sqlite3_stmt *statement = nil;
int result = sqlite3_prepare_v2(database, sql, -1, &statement, NULL);
if (result != SQLITE_OK) {
sqlite3_close(database);
return NO;
}
result = sqlite3_bind_int(statement, 1, id_min);
if (result != SQLITE_OK) {
sqlite3_finalize(statement);
sqlite3_close(database);
return NO;
}
while (sqlite3_step(statement) == SQLITE_ROW) {
int id = sqlite3_column_int(statement, 0);
NSString *name = nil;
const unsigned char *str = sqlite3_column_text(statement, 1);
if (str && strlen((const char *)str)) {
name = [NSString stringWithUTF8String:(const char *)str];
}
// id, nameをどこかに格納する.
}
result = sqlite3_finalize(statement);
if (result != SQLITE_OK) {
sqlite3_close(database);
return NO;
}
result = sqlite3_close(database);
if (result != SQLITE_OK) {
return NO;
}
return YES;