Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6631175
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:30:33+00:00 2026-05-25T22:30:33+00:00

i have a SQL file where 5 different type of data is stored. I

  • 0

i have a SQL file where 5 different type of data is stored. I am adding this data in a dictionary specified with keys. and then i am adding this dictionary to tableData array as a dataSource for table and searchBar. But it is not searching anything.

adding code below

- (void)viewDidLoad {
[super viewDidLoad];


dataSource =[[NSMutableArray alloc] init];
tableData = [[NSMutableArray alloc]init];
searchedData = [[NSMutableArray alloc]init];


NSString *query = [NSString stringWithFormat:@"SELECT * FROM Vegetables"];

SQLite *sqlObj1 = [[SQLite alloc] initWithSQLFile:@"ShoppersWorld.sqlite"];
[sqlObj1 openDb];
[sqlObj1 readDb:query];

    //  [query release];
for (int i=0; i<[dataSource count]; i++) {
    NSLog(@"data:%@",[dataSource objectAtIndex:i]);
}

while ([sqlObj1 hasNextRow]) 
{

    NSString *name=[sqlObj1 getColumn:1 type:@"text"];
    NSString *price=[sqlObj1 getColumn:2 type:@"text"];
    NSString *quantity=[sqlObj1 getColumn:3 type:@"text"];
    NSString *unit=[sqlObj1 getColumn:4 type:@"text"];
    NSString *total=[sqlObj1 getColumn:5 type:@"text"]; 


    dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys: name,@"nameOfVegetables",
                                                                price,@"priceOfVegetables",
                                                                quantity,@"quantityOfVegetables",
                                                                unit,@"unitOfVegetables",
                                                                total,@"totalPriceOfVegetables",nil];


        //NSLog(@"results:%@ %@",dict);
    [dataSource addObject:dict];


}

[tableData  addObjectsFromArray:dataSource];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @"Cell";

CustomCell *cell = (CustomCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[CustomCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];

}

    // Set up the cell...
    // Configure the cell.


else {

    cell.productLbl.text= [NSString stringWithFormat:@"%@",[[tableData objectAtIndex:indexPath.row]objectForKey:@"nameOfVegetables"] ];     
    cell.bPriceLbl.text = [NSString stringWithFormat:@"Rs %d/Kg",
                                        [[[tableData objectAtIndex:indexPath.row] objectForKey:@"priceOfVegetables"] intValue]]; 

    cell.qtyLbl.text = [NSString stringWithFormat:@"QTY: %@ %@",[[tableData objectAtIndex:indexPath.row] 
                                                               objectForKey:@"quantityOfVegetables"],[[tableData objectAtIndex:indexPath.row] objectForKey:@"unitOfVegetables"]] ;

    cell.tPriceLbl.text = [NSString stringWithFormat:@"TOTAL: %@",[[tableData objectAtIndex:indexPath.row]
                                                                    objectForKey:@"totalPriceOfVegetables"]];

}
return cell;
}

#pragma search operations

- (IBAction)search:(id)sender{

sBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0,40,320,30)];
sBar.delegate = self;
[self.view addSubview:sBar];

}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{

    // only show the status bar’s cancel button while in edit mode
[sBar setShowsCancelButton:YES animated:YES];
sBar.autocorrectionType = UITextAutocorrectionTypeNo;
    // flush the previous search content
[tableData removeAllObjects];
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{

[sBar setShowsCancelButton:NO animated:YES];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

    [tableData removeAllObjects];// remove all data that belongs to previous search
    if([searchText isEqualToString:@""] || searchText==nil){
        [tableview reloadData];
        return;

}
NSInteger counter = 0;
for(NSString *name in dataSource)

    for (int i = 0; i < [dataSource count]; i++)
    {
        NSMutableDictionary *temp = (NSMutableDictionary*) [dataSource objectAtIndex:i];
        NSString *name = [NSString stringWithFormat:@"%@", [temp valueForKey:@"nameOfVegetables"]];
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];      
        NSRange r = [name rangeOfString:searchText options:NSCaseInsensitiveSearch];        
        if(r.location != NSNotFound)            
        {           
            if(r.location== 0)//that is we are checking only the start of the names.                
            {               
                [tableData addObject:name];             
            }           
        }       
        counter++;      
        [pool release];
    }

[tableview reloadData];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{

sBar.hidden= YES;
    // if a valid search was entered but the user wanted to cancel, bring back the main list content
[tableData removeAllObjects];
[tableData addObjectsFromArray:dataSource];
@try{
    [tableview reloadData];
}
@catch(NSException *e){
}
[sBar resignFirstResponder];
sBar.text = @"";
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:30:34+00:00Added an answer on May 25, 2026 at 10:30 pm

    In search delegate methods you manipulate not with searchedData but tableData array. As these name suggest, array searchedData is supposed to store filtered data.

    By the way, your approach to use sqlite for data source and absorbing all database into array is wrong. In cellForRowAtIndexPath read from sqlite database only data you need at the moment.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
       // load from sqlite only data for this cell.
       // Use searchBar.text with sqlite's LIKE to filter data from database
       NSUInteger row = [indexPath row];
       static NSString *CellIdentifier = @"SignsCellIdentifier";
       UITableViewCell *cell = [table dequeueReusableCellWithIdentifier:CellIdentifier];
       if (cell == nil) {                                       
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
        }
        NSString *sql = [NSString stringWithFormat:@"SELECT fieldNameFromTable FROM TableName WHERE FieldToLookForIn LIKE \"%%%@%%\" LIMIT 1 OFFSET %u", searchBar.text ? searchBar.text : @"", row];
        sqlite3_stmt *stmt;
        int res = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
        if (res != SQLITE_OK) {
            NSLog(@"sqlite3_prepare_v2() failed"];
            return nil;
        }
    
        if (sqlite3_step(stmt) == SQLITE_ROW) {
            const unsigned char *name = sqlite3_column_text(stmt, 0);            
            cell.text = [NSString stringWithUTF8String:(const char*)name];
        }
        sqlite3_finalize(stmt);
    return cell;
    }
    

    How to apply search in this approach? In textDidChange do nothing but call [tableView reloadData]. And in cellForRowAtIndexPath load data with sqlite LIKE using searchBar.text as search term. So reloadData will load only filtered records. searchBarSearchButtonClicked will call only resignFirstResponder of it’s caller, removing keyboard off screen. It doesn’t need to do anything more because search is already done. searchBarCancelButtonClicked will set text property of it’s caller to nil, call reload data and again call resignFirstResponder.

    - (void)searchBarCancelButtonClicked:(UISearchBar *)s {
        s.text = nil;
        [tableView reloadData];
        [s resignFirstResponder];
    }
    
    - (void)searchBarSearchButtonClicked:(UISearchBar *)s {
        // search is already done
        [s resignFirstResponder];
    }
    
    - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
        [tableView reloadData];
    }
    

    numberOfRowsInSection should also request db with the same SELECT as in cellForRowAtIndexPath, but with SELECT COUNT. Writing this method will be your homework)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .sql file from a MySQL dump containing tables, definitions and data
I have a SQL dump file consisting of incorrectly stored Cyrillic Russian ( WINDOWS-1251
I have a sql file generated by mysqldump --all-databases . There are many databases
I have a SqlDump.sql file that works just fine when I apply it using
So I have an SQL dump file that needs to be loaded using mysql_query().
I have a rather large SQL file which starts with the byte order marker
I have an application that has a .sql file in it. The sql file
I've got two PostgreSQL databases that have been created using the same sql file.
I have a full MS SQL Backup file that I would like to extract
I have a SQL procedure that generates a util file as its output depending

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.