UITableView not getting populated with sqlite database column

Posted by Ingila Ejaz on Stack Overflow See other posts from Stack Overflow or by Ingila Ejaz
Published on 2012-06-10T10:35:28Z Indexed on 2012/06/10 10:40 UTC
Read the original article Hit count: 200

Here's the code of a method I used to populate UITableView with the contents of a column in sqlite database. However, this is giving no errors at run-time but still does not give any data in UITableView. If anyone could help, it'll be highly appreciated.

- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"Strings List";
    UITableViewCell *cell;
    NSString *docsDir;
    NSArray *dirPaths;
    sqlite3_stmt *statement;
    // Get the documents directory
    dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    docsDir = [dirPaths objectAtIndex:0];

    // Build the path to the database file
    databasePath = [[NSString alloc]initWithString: [docsDir stringByAppendingPathComponent:@"strings.sqlite"]];

    NSFileManager *filemgr = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: databasePath ] == NO) 
    {
        const char *dbpath = [databasePath UTF8String];
        if (sqlite3_open(dbpath, &contactDB) == SQLITE_OK)
        {
        NSString * query =
            @"SELECT string FROM strings";
            const char * stmt = [query UTF8String];
            if (sqlite3_prepare_v2(contactDB, stmt, -1,&statement, NULL)==SQLITE_OK){
                if (sqlite3_step(statement) == SQLITE_ROW)
                {
    NSString *string = [[NSString alloc]initWithUTF8String:(const char*)sqlite3_column_text(statement, 0)];
                    cell.textLabel.text = string;                }
            }
            else NSLog(@"Failed");
            sqlite3_finalize(statement);
       }
       sqlite3_close(contactDB);
    }

}

© Stack Overflow or respective owner

Related posts about iphone

Related posts about objective-c