UIPickerView で選択した値に基づいて SQL ステートメントを作成する必要があります。視覚的なアイデアが必要な場合は、スクリーンショットへのリンクを次に示します(まだ写真を投稿するには評判が不十分です)。これに関するドキュメントを見つけることができませんでした。掘り下げる前に、正しい方向に進んでいることを確認したいと思います。
各コンポーネント (kTypeComponent、kDifficultyComponent、kDescriptionComponent) には、選択できる 3 つの行があります (例: kTypeComponent row1=bike、row2=run、row3=swim)。
私の考えでは、SQLステートメントは次のようになります
sqlite3_stmt *pickerStatement;
//This would give back a string of the row selected (i.e bike, run, swim)
NSInteger getTypeSelected = [pickerView selectedRowInComponent:kTypeComponent];
NSString typeSQL = [rowOneItems objectAtIndex:getTypeSelected];
const char *pickerSQL = "SELECT description FROM workoutTbl WHERE (type = typeSQL) AND ...
これはSQLステートメントで行うことができますか? 基本的なSQLしか知らないのでよくわかりません
SQL ステートメントはアクション (ボタン) に入れますか、それとも NSMutableArray をセットアップしてデータベースを開く場所に入れますか? 別のクラスに入る必要がありますか?
編集 - 解決策
誰かが同じ問題に遭遇した場合、ここに解決策があります
- (NSArray *)getWorkoutListwithType:(NSString *)workoutType withDifficulty:(NSString *)difficulty withLength:(NSString *)length {
NSMutableArray *workouts;
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"workoutList.sqlite"];
// NSLog(@"Db path is %@",dbPath);
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if (!success){
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if (!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)) {
NSLog(@"error with message '%s'.", sqlite3_errmsg(db));
}
// only alloc/init the array if the SQL database opens properly
workouts = [[NSMutableArray alloc] init];
sqlite3_stmt *sqlStatement;
// add "%%" as a wildcard so the query will say "difficulty LIKE '>30%' and match >30 MINS, >30 HOURS, etc.
NSString *sqlString = [NSString stringWithFormat: @"SELECT description FROM workoutTbl WHERE type LIKE '%@%%' AND difficulty LIKE '%@%%' AND duration LIKE '%@%%'", workoutType, difficulty, length];
NSLog(@"query: %@", sqlString);
const char *sql = [sqlString UTF8String];
if (sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK) {
NSLog(@"%s Prepare failure '%s' (%1d)", __FUNCTION__, sqlite3_errmsg(db), sqlite3_errcode(db));
}
while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
[workouts addObject:[NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,0)]];
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
@finally {
sqlite3_close(db);
}
// Pass back an immutable copy of the array. if the array is nil, then the database never opened and there will be an error
return [workouts copy];
}