【问题标题】:sql and UIPickerViewsql 和 UIPickerView
【发布时间】:2013-02-11 15:36:02
【问题描述】:

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

每个组件(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];

}

【问题讨论】:

    标签: ios objective-c sql sqlite uipickerview


    【解决方案1】:

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

    NSString* sqlStatement = [NSString stringWithFormat:@"SELECT * FROM workoutTbl WHERE type = '%@' AND id = '%i'", typeSQL,idNumber];
    

    【讨论】:

    • 太棒了。这给了我我需要的确切的 sql 语句。我是否必须将其转换为 const char 才能在我的准备语句中使用它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多