0

In my app I populate a NSMutableArray from a .sqlite database. Inside this array where I grab all the goodies from the database, I run a query to look inside the database. However, I want to be able to change this query dependent on what user selects in UITableview controller prior to this view controller. You'll see the query inside the function:

 NSString *character = @"SELECT * FROM strengthexercises WHERE primarymuscle LIKE  '%bicep%'";
    const char *sql = [character cStringUsingEncoding:NSASCIIStringEncoding];

That lies within the array method:

    -(NSMutableArray *) authorList{
    _theauthors = [[NSMutableArray alloc] initWithCapacity:10];
    @try {
    NSFileManager *fileMgr = [NSFileManager defaultManager];
    NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"StayhealthyExercises.sqlite"];
    BOOL success = [fileMgr fileExistsAtPath:dbPath];
    if(!success)
    {
        NSLog(@"Cannot locate database file '%@'.", dbPath);
    }
    if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
    {
        NSLog(@"An error has occured: %s", sqlite3_errmsg(db));

    }

    NSString *character = @"SELECT * FROM strengthexercises WHERE primarymuscle LIKE '%bicep%'";
    const char *sql = [character cStringUsingEncoding:NSASCIIStringEncoding];

    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
    {
        NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
    }else{

        while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
            sqlColumns * author = [[sqlColumns alloc] init];
            author.Name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
            author.Muscle = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,2)];
            author.Description = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
            author.File= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
            author.Sets= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 5)];
            author.Reps= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 6)];
            author.Equipment= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 7)];
            author.PrimaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 8)];
            author.SecondaryMuscle= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 9)];
            author.Difficulty= [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 10)];

            [_theauthors addObject:author];
        }
    }
    sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
    NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
    sqlite3_close(db);

    return _theauthors;
}
}

Now you should see my problem, the query is directly inside the array, I want to be able to control what the query is dependent on what the user selected in the UITableview controller before.

9
  • And when you run it what u get ? I mean what is the question ? where is the problem ? Dec 22, 2013 at 0:03
  • In didSelectRowAtIndexPath (you can know what user selected) then inside this function you can do whatever you want , extract string from your array and send it to function that will do what needed. Dec 22, 2013 at 0:08
  • Please be specific about your problem. It appears that you are trying to change your query text based on the selection of a table cell. But where is the actual issue in that? Dec 22, 2013 at 0:21
  • @AyanSengupta Yes your right I want to know how you do what you said could you give me an example? Dec 22, 2013 at 0:56
  • @Roma-MT Yes I agree, could you give an example? Dec 22, 2013 at 0:57

1 Answer 1

2

Table view delegate didSelectRowAtIndexPath triggers when user clicks on row in tableView assuming you do have a tableView .

inside this function you can "query" what row was selected by switching indexPath.row

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
  NSInteger row = indexPath.row;
  //lest examine the selected row 
  switch(row)
  {
      case 0:
      {
         //call function with string (for 0)
         break;
      }
      case 1:
      {
         //call function with string (for 1)
         break;
      }
      //etc..
  }
}

also I assume you do have or can make this function that will lets say get integer and trigger the needed sql query.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.