Note that there are some explanatory texts on larger screens.

plurals
  1. POsql and UIPickerView
    primarykey
    data
    text
    <p>I need to develop a sql statement based on values picked on a UIPickerView. If you need a visual idea, here's a <a href="http://www.corinnpope.com/?attachment_id=1044" rel="nofollow">link</a> to the screenshot (sorry not enough reputation to post pics yet) . I haven't been able to find any documentation on this and want to make sure I'm on the right track before I dig into it. </p> <p>Each component (kTypeComponent, kDifficultyComponent, kDescriptionComponent) has three rows to select from (ex. kTypeComponent row1=bike, row2=run, row3=swim)</p> <p>My thought would be that the sql statement would look something like this</p> <pre><code> 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 ... </code></pre> <p>Is this possible to do with a sql statement? I'm only familiar with basic SQL, so I'm not sure </p> <p>Would the SQL statement go in the action (button) or where I set up my NSMutableArray and open the database? Should it go into a different class? </p> <p><strong>Edit - Solution</strong></p> <p>In case anyone comes around with the same problem, here is the solution to it</p> <pre><code> - (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], &amp;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 '&gt;30%' and match &gt;30 MINS, &gt;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, &amp;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]; </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload