Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an NSMutableArray that I want to populate with an sqlite database and display the data in a table view. The app builds and shows "database opened" in the console but just shows an empty tableview, I had it working perfectly but suddenly it seems to have stopped working. here is the code for adding the data to the array:

    #import "ViewController.h"

    @interface ViewController ()

    @end

    @implementation ViewController
    @synthesize entries;

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //[entries addObject:@"object1"];
        //[entries addObject:@"object2"];
        //[entries addObject:@"object3"];
        //[entries addObject:@"object4"];
        [self openDB];
        entries = [[NSMutableArray alloc] init];
        NSString *sql = [NSString stringWithFormat:@"SELECT * FROM speakers ORDER BY name"];
        sqlite3_stmt *statement;

        if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
        {
            while (sqlite3_step(statement)==SQLITE_ROW) {

                char *field2 = (char *) sqlite3_column_text(statement,1);
                NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];

                NSString *str = [[NSString alloc]initWithFormat:@"%@", field2Str];
                [entries addObject:str ];


            }
        }
    }
//file path to db
-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains
    (NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0] stringByAppendingPathComponent:@"speakerdb.sqlite"];
}

//open the db
-(void)openDB {
    if (sqlite3_open([[self filePath] UTF8String], &db) != SQLITE_OK){
        sqlite3_close(db);
        NSAssert(0,@"error preparing statement: %s", sqlite3_errmsg(db));
    }else{
        NSLog(@"database opened");
    }
}

The NSMutableArray is defined in the header file as follows:

#import <UIKit/UIKit.h>
#import "sqlite3.h"
#import "speakerViewController.h"

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    IBOutlet UITableView *theTable;
    sqlite3 *db;
}
@property (nonatomic,retain) NSMutableArray *entries;
@end

Here is the code for adding the data to the table view:

-(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 [entries count];
}

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

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    //Congigure the cell...
    cell.textLabel.text = [entries objectAtIndex:indexPath.row];
    return cell;
}

The data shows in the table view if I add objects to the array manually (commented out lines shown in the viewDidLoad method) but doesnt seem to work from the sqlite database.

Any help would be much appreciated.

Edit: I'm using xcode 5.1.1

share|improve this question
    
char *field2 = (char *) sqlite3_column_text(statement,0); –  incmiko Jun 15 at 20:12
    
thanks but that didn't work, think it needs to be 1 as its the second column i want to display –  Will Jun 15 at 20:16
    
Check my edited answer –  incmiko Jun 15 at 20:23
add comment

1 Answer 1

up vote 1 down vote accepted

Try to debug your code... Put a break point on NSString *field2Str = [[NSString alloc]initWithUTF8String:field2]; row, and check that field2 is not empty...

try to open db like this:

 NSString *sqLiteDb = [[NSBundle mainBundle] pathForResource:@"speakerdb"
                                                             ofType:@"sqlite"];

        if (sqlite3_open([sqLiteDb UTF8String], &_database) != SQLITE_OK) {
            NSLog(@"Failed to open database!");
        }

And one thing is missing from your code:

    sqlite3_finalize(statement);

use it like this:

if(sqlite3_prepare_v2(db, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
        {
            while (sqlite3_step(statement)==SQLITE_ROW) {

                char *field2 = (char *) sqlite3_column_text(statement,1);
                NSString *field2Str = [[NSString alloc]initWithUTF8String:field2];

                NSString *str = [[NSString alloc]initWithFormat:@"%@", field2Str];
                [entries addObject:str ];


            }
        sqlite3_finalize(statement);

        }
share|improve this answer
    
Thank you so much! adding those changes has worked! Think it was the missing sqlite3_finalize(statement); that did the trick –  Will Jun 15 at 20:27
    
I'm glad to help you –  incmiko Jun 15 at 20:29
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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