-1

I have an SQLite database named "BookDataBase.sqlite" with two columns : Category , Name .

It contains categories for books and some book names for each category .

I have a table view controller connected to class "CategoryTableViewController" which is a subclass of "UITableViewController" .

I want the table view to display the result for a query from the database where only the categories are displayed .

I have tried this code but it didn't work for me .

CategoryTableViewController.m :

#import "CategoryTableViewController.h"
#import <sqlite3.h>

@interface CategoryTableViewController ()

@end

sqlite3 *BookDatabase;
NSString *databasepath;
const char *filepath;
NSString *querySQL;
const char *query_stmt;
sqlite3_stmt *statement;
NSMutableDictionary *BookRecord;
NSUserDefaults *BookData;
NSMutableArray *BookArray;


@implementation CategoryTableViewController

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

-(void) viewWillAppear:(BOOL)animated {

    databasepath = [[NSBundle mainBundle]pathForResource:@"BookDatabase" ofType:@"sqlite"];
    filepath = [databasepath UTF8String];

    if (sqlite3_open(filepath, &BookDatabase) == SQLITE_OK) {


        querySQL = @"SELECT Category FROM BookTable Group By Categoy" ;
        query_stmt = [querySQL UTF8String];

        if (sqlite3_prepare_v2(BookDatabase, query_stmt, -1, &statement, NULL) == SQLITE_OK) {
            while (sqlite3_step(statement) == SQLITE_ROW) {

                BookRecord =[[NSMutableDictionary alloc] initWithCapacity:sqlite3_column_count(statement)];

                for (int i=0; i<sqlite3_column_count(statement); i++) {

                    NSString *ColName = [[NSString alloc] initWithUTF8String:sqlite3_column_name(statement, i)];
                    NSString *ColVal = [[NSString alloc] initWithUTF8String:(char *)sqlite3_column_value(statement, i)];




                    [BookRecord setObject:ColVal forKey:ColName];
                }


                [BookArray addObject:BookRecord];



            }


             sqlite3_finalize(statement);

        }

        sqlite3_close(BookDatabase);





    }





}




- (void)viewDidLoad
{
    [super viewDidLoad];

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [BookArray count];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"CategoryTableViewCell";
   UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];





    // Configure the cell...

    int row = [indexPath row];



    cell.textLabel.text = [BookArray[row] objectForKey:@"Category"];




    return cell;
}

@end

I have already added the libsqlite3 framework.

The database file is located in the app folder :

https://dl-web.dropbox.com/get/Screen%20Shot%202014-08-15%20at%207.18.22%20PM.png?_subject_uid=104745578&w=AADDzW343bbSsnQ9wko1dFQ3yQes8N5tY9hJmEDb3qnwNw

The type for the table view prototype cell is set to basic and the cell identifier is "CategoryTableViewCell".

The name of the table in the database is "BookTable".

Can someone please help me fix this?

Feel free to rewrite my entire code if needed < but please keep in mind that it's a subcalass of "UITableViewController".

Thanks .

11
  • You wrote "it didn't work" - how exactly it does not work? Does it reads the data from the DB or do you have an error message? Aug 15, 2014 at 14:55
  • @AlexAtNet The tableview is empty with nothing in it.
    – MhdSamad
    Aug 15, 2014 at 15:05
  • Can you put a breakpoint on [BookRecord setObject:ColVal forKey:ColName]; and check that the data is really added to the tableview? Aug 15, 2014 at 15:38
  • So, what have you done to debug this? Do you even know if the DB is being successfully read? Do you even know if the DB file exists?
    – Hot Licks
    Aug 15, 2014 at 15:43
  • @AlexAtNet I put a breakpoint, and nothing turned up .
    – MhdSamad
    Aug 15, 2014 at 16:09

1 Answer 1

0

I FINALLY FOUND IT

Turns out I didn't add the database file to the main bundle .

I didn't know I should do that .

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.