Search code examples
iossqliteios5ios6fmdb

display data from sqlite to label using FMDB


How to display data from SQLite to label using FMDB like this:

enter image description here

I would like to display whatever the output is in this format for the user to see. No Interaction. Only display for the select query.


Solution

  • If you want to add UILabel objects, just iterate through your result set, adding labels to your view. Something like:

    NSInteger rowNum = 0;
    NSArray *columnWidths = @[@(50), @(120), @(75), @(75)];
    CGFloat rowHeight = 24.0;
    
    FMResultSet *rs = [db executeQuery:sql];
    
    while ([rs next])
    {
        CGFloat x = 0.0;
        NSInteger colNum = 0;
        for (NSNumber *columnWidth in columnWidths)
        {
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x,
                                                                        rowHeight * rowNum, 
                                                                        [columnWidth floatValue], 
                                                                        rowHeight)];
            label.textAlignment = NSTextAlignmentCenter;
            label.layer.borderColor = [UIColor blackColor].CGColor;
            label.layer.borderWidth = 1.0;
            label.text = rs[colNum++];
            [self.view addSubview:label];
            x += [columnWidth floatValue];
        }
        rowNum++;
    }
    
    [rs close];
    

    I just typed that in, so I'm sure there are typos in there, but hopefully it's enough to give you an idea of what it could look like. If there are too many rows to fit on a screen, you would add a scrollview to your view, and then add the labels as a subview to that.

    I also think the tableview is a wonderful approach. Or dynamically built a HTML string that you show in a UIWebView. Tons of options here. It depends a little upon what your ultimate goal is here, the nature of the data, what sort of user interaction you want.