0

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);
    }

}
Ilanchezhian
  • 17,426
  • 1
  • 53
  • 55
Ingila Ejaz
  • 399
  • 7
  • 25

4 Answers4

0

I recommend you to read through Table View Programming Guide.

Most of the sequence you put in the viewDidLoad normally is placed in

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath; 
barley
  • 4,403
  • 25
  • 28
  • I know that's placed in that but that's in case when a person has a strings class to hold strings object that can be used inside this method. I've not made any strings class that's why i'm confused :( – Ingila Ejaz Jun 10 '12 at 11:42
0

that pretty complex method u are using, when u have all the delegate methods for table view which is just like putting butter on bread.

Try My Answer from this Link

Let me know if it worked !!!!
Cheers

Community
  • 1
  • 1
WaaleedKhan
  • 685
  • 7
  • 18
  • @BarinStroms in the link you mentioned only the app deligates, what I'm stuck at has something to do ith uitableview :( – Ingila Ejaz Jun 10 '12 at 11:48
  • what i mentioned in the answer is there some times exist a problem with DB and Xcode, It may not be connected properly. beside the approach i have described is best known when handling Databases. – WaaleedKhan Jun 10 '12 at 15:12
0

if You are using simulator for testing purpose then delete your previous build and install app again. It fetch SQL file from document directory. So please try this and let me know. Thanks

0

Have you check if the problem is with you tableview or database? Are you able to fetch data from database?Check if you are missing any method to connect sqlite in code.

Dhruv
  • 2,153
  • 3
  • 21
  • 45