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 8582389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:16:45+00:00 2026-06-11T21:16:45+00:00

I was trying to create a search engine on Xcode 4.3.3, a search bar

  • 0

I was trying to create a search engine on Xcode 4.3.3, a search bar searching a UITableView.

First I tried to create the search engine with no dynamic database and no sqlite connection, it worked and i was capable if searching between static elements predefined in a NSMutableArray.

Now instead of having the elements statically predefined in the array, I want to access a sqlite database and execute a query, having the results as cell fields.
I have tried to connect using FMDB, but the problem is I cannot realize that.

Here is my code, what am I doing wrong?

//
//  FilterDemoTableViewController.m
//  Created by Elias Rahme.
//  Copyright (c) ERC.
//

#import "ViewController.h"
#import "DetailsViewController.h"
#import "Food.h"
#import "FMDatabase.h"

@implementation ViewController

@synthesize allTableData;
@synthesize filteredTableData;
@synthesize searchBar;
@synthesize isFiltered;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];

    searchBar.delegate = (id)self;
    NSString *path  = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"];
    FMDatabase *db  = [[FMDatabase alloc] initWithPath:path];
    [db open];
    if (![db open]) {
        NSLog(@"Could not open db.");
    }
    else{
    NSLog(@"Database is opened");
    }
    FMResultSet *fResult= [db executeQuery:@"SELECT a_id FROM Description where desc_id=1"];
    if( !fResult){

        NSLog(@"Could not execute query");
      }
    else{
        NSLog(@"Query executed");
    }
    eleData = [fResult stringForColumn:@"desc"];

  //  NSLog([fResult stringForColumn:@"desc"]);

    allTableData = [[NSMutableArray alloc] initWithObjects:
                    [[Food alloc] initWithName:@"test" andDescription:@"TEst" ], 
                    [[Food alloc] initWithName:@"Steak" andDescription:@"Medium"], 
                    [[Food alloc] initWithName:@"Salad" andDescription:@"Caesar"], 
                    [[Food alloc] initWithName:@"Salad" andDescription:@"Bean"], 
                    [[Food alloc] initWithName:@"Fruit" andDescription:@"Apple"], 
                    [[Food alloc] initWithName:@"Potato" andDescription:@"Baked"], 
                    [[Food alloc] initWithName:@"Potato" andDescription:@"Mashed"], 
                    [[Food alloc] initWithName:@"Bread" andDescription:@"White"], 
                    [[Food alloc] initWithName:@"Bread" andDescription:@"Brown"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Beef"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Chicken"], 
                    [[Food alloc] initWithName:@"Hot Dog" andDescription:@"Veggie"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    [[Food alloc] initWithName:@"Pizza" andDescription:@"Pepperonni"], 
                    nil ];
    [db close];

}
 /*   
    NSLog(@"Data from ele.h file");


    /* Now here we have to write the code from the database to display the details */
    /*
    aElephant = [[NSMutableArray alloc] init];
    NSLog(@"Data base is entering");

    /*
     Here we have to set the path 
     We are creating the path for the datbase file in order to access
     */
/*  
    NSString *path  = [[NSBundle mainBundle] pathForResource:@"SampleDB" ofType:@"sqlite"];

    NSLog(@"No problem with the path");

    /* FMDatabase here is used to take the database path */
/*  
    FMDatabase *db  = [[FMDatabase alloc] initWithPath:path];

    /* Here we are opening the datase in order to access it */
/*  
    [db open];
    NSLog(@"Database Opened");

    /*
     Then the next step here is taking the database result using the sqlquery and then carrying that result into the resultset object   
     Resultset for Elephant: select * from Description where a_id=1; here description is the table, a_id is the id of Elephant it is
     the primary key in animals table
/*   */
/*  NSLog(@"NOt yet executing the query");
    FMResultSet *fResult= [db executeQuery:@"SELECT * FROM description"];
    NSLog(@"Did Execute the Query");
  //  NSLog(*fResult);

    //Now we have to take these results in to a loop and then repeteadly loop it in order to get all the data in a our Array Object

    while([fResult next])
    {
    /*  /* taking results from database to a string "eleData" */
    /*  eleData = [fResult stringForColumn:@"desc"];
        /* adding data from the string object to Array */
        //[aElephant addObject:eleData];
        /* Checking weather data has come or not */
    //  NSLog(@"The data is %@=",eleData);
    //}
    /* Closing the Database */
    //[db close];


- (void)viewDidUnload
{
    [self setSearchBar:nil];
    [super viewDidUnload];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    int rowCount;
    if(self.isFiltered)
        rowCount = filteredTableData.count;
    else
        rowCount = allTableData.count;

    return rowCount;
   // return [aElephant count];

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

      /*
    if(isFiltered)
        eData = [filteredTableData objectAtIndex:indexPath.row];
    else
        eData = [allTableData objectAtIndex:indexPath.row];
*/
  //  NSLog(aElephant);
 /*   NSString *eData = [aElephant objectAtIndex:indexPath.row];
    cell.textLabel.text  = eData;
    return cell;*/


    Food* food;
    if(isFiltered)
        food = [filteredTableData objectAtIndex:indexPath.row];
    else
        food = [allTableData objectAtIndex:indexPath.row];

    cell.textLabel.text = food.name;
    cell.detailTextLabel.text = food.description;


    return cell;
}

#pragma mark - Table view delegate

-(void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)text
{
    if(text.length == 0)
    {
        isFiltered = FALSE;
    }
    else
    {
        isFiltered = true;
        filteredTableData = [[NSMutableArray alloc] init];

        for (Food* food in allTableData)
        {
            NSRange nameRange = [food.name rangeOfString:text options:NSCaseInsensitiveSearch];
            NSRange descriptionRange = [food.description rangeOfString:text options:NSCaseInsensitiveSearch];
            if(nameRange.location != NSNotFound || descriptionRange.location != NSNotFound)
            {
                [filteredTableData addObject:food];
            }   
        }
    }

    [self.tableView reloadData];
}


- (void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath
{
    [self showDetailsForIndexPath:indexPath];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self showDetailsForIndexPath:indexPath];
}

-(void) showDetailsForIndexPath:(NSIndexPath*)indexPath
{
    [self.searchBar resignFirstResponder];
    DetailsViewController* vc = [self.storyboard instantiateViewControllerWithIdentifier:@"DetailsViewController"];
    Food* food;

    if(isFiltered)
    {
        food = [filteredTableData objectAtIndex:indexPath.row];
    }
    else
    {
        food = [allTableData objectAtIndex:indexPath.row];
    }

    vc.food = food;
    [self.navigationController pushViewController:vc animated:true];    
}

@end

In short, instead of initWithName having a static value, i want it to have a value from the sqlite database.
Any help will be highly appreciated
Thank you for helping.

  • 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-06-11T21:16:46+00:00Added an answer on June 11, 2026 at 9:16 pm

    The problem was i didn’t back then access the database correctly , and did not see the changes happening because i was opening my local database when i should be opening the one in iPhone simulator in my documents.

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

Sidebar

Related Questions

I am trying to create a search bar for website which searches the database
I have been trying to create a new google custom search engine, but when
I'm trying to create a small search for my site. I've tried using full-text
I am trying to create search engine friendly URLs with the following .htaccess and
Using: Rails 3.1.1 I am trying to create a search engine in my application
I'm trying to create a search engine for all literature (books, articles, etc), music,
I'm trying to create my own search engine for a site I'm working on
I'm desperately trying to create a relatively concise search engine with PHP, mySQL and
I am trying to create a search box to look for invoices in database.
I'm trying to create a search engine for an inventory based site. The issue

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.