0

我需要根据 UIPickerView 上选取的值开发一个 sql 语句。如果您需要视觉创意,这里是截图的链接(抱歉,还没有足够的声誉来发布图片)。我无法找到任何有关此的文档,并希望在深入研究之前确保我走在正确的轨道上。

每个组件(kTypeComponent、kDifficultyComponent、kDescriptionComponent)都有三行可供选择(例如 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];

}

4

1 回答 1

0

“三行选择”是什么意思?您的意思是“要选择的三个字段(列)”吗?如果你想指定字段值,那么语句应该像

NSString* sqlStatement = [NSString stringWithFormat:@"SELECT * FROM workoutTbl WHERE type = '%@' AND id = '%i'", typeSQL,idNumber];
于 2013-02-11T22:18:18.380 回答