How to Display data from SQlite into Table views to iPhone app

走远了吗. 提交于 2019-12-05 05:38:56

I suggest a light wrapper over SQLite - see https://github.com/JohnGoodstadt/EasySQLite

This will allow:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _personTable.rows.count;
}

AND

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ...

     NSArray* row= _personTable.rows[indexPath.row];
     cell.textLabel.text = row[[_personTable colIndex:@"lastname"]];
     ...

Set this up by using an iVar representing a SQL Table:

self.personTable = [_db  ExecuteQuery:@"SELECT firstname , lastname , age , salary FROM person"];

And a DB Connection iVar passing in your SQL file name:

self.db = [DBController sharedDatabaseController:@"DataTable.sqlite"];

First of all, I suggest using FMDB, which is an Objective-C wrapper around sqlite3. Secondly, I'd create a custom Data Access Object with a shared instance, like so:

@interface MyDatabaseDAO : NSObject
    @property (nonatomic, strong) FMDatabase *database;
@end


@implementation MyDatabaseDAO
@synthesize database = _database;

+ (MyDatabaseDAO *)instance {
    static MyDatabaseDAO *_instance = nil;

    @synchronized (self) {
        if (_instance == nil) {
            _instance = [[self alloc] init];
        }
    }

    return _instance;
}

- (id)init {
    self.database = [FMDatabase databaseWithPath:myDatabasePath];
    [self.database open];
}

- (void)dealloc {
    [self.database close];
}
@end

This DAO must have 3 access methods: one for each data object in the database. Because you weren't specific, I made these objects without any specific properties.

- (NSArray *)retrieveAllFirstViewItems {
    NSMutableArray *items = [NSMutableArray array];
    FMResultSet *resultSet = [FMDBDatabase.database executeQuery:@"SELECT * FROM myFirstViewItemTable"];    

    while ([resultSet next]) {
        // extract whatever data you want from the resultset
        NSString *name = [resultSet stringForColumn:@"name"]
        [items addObject:name];
    }
    [resultSet close];

    return items;
}

- (MySecondViewItem *)retrieveSecondViewItemFromIndexPath:(NSIndexPath *)indexPath {

    FMResultSet *resultSet = [FMDBDatabase.database executeQuery:@"SELECT * FROM mySecondViewItemTable WHERE pid = ?", [indexPath indexAtPosition:0]];
    if ([resultSet next]) {
        // extract whatever data you want from the resultset
        NSString *name = [resultSet stringForColumn:@"name"]
        MySecondViewItem *mySecondViewItem = [[MySecondViewItem alloc]
                initWithName:name withPID:[indexPath indexAtPosition:0]];
        [resultSet close];
        return mySecondViewItem;
    } else {
        return nil;
    }
}

- (MyThirdViewItem *)retrieveThirdViewItemFromIndexPath:(NSIndexPath *)indexPath {

    FMResultSet *resultSet = [FMDBDatabase.database executeQuery:@"SELECT * FROM mySecondViewItemTable WHERE pid = ?", [indexPath indexAtPosition:1]];
    if ([resultSet next]) {
        // extract whatever data you want from the resultset
        NSString *name = [resultSet stringForColumn:@"name"]
        MyThirdViewItem *myThirdViewItem = [[MyThirdViewItem alloc]
                initWithName:name withPID:[indexPath indexAtPosition:1]];
        [resultSet close];
        return myThirdViewItem;
    } else {
        return nil;
    }
}

Since it's read-only, these are all the required methods. In your first UITableView, just implement method:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    MySecondViewItem *mySecondViewItem = [[MyDatabaseDAO instance] retrieveSecondViewItemFromIndexPath:indexPath];
    //instantiate a view from this item and use [UINavigationController pushViewController:animated:] to navigate to it
}

All that's left is just to show your data objects in views somehow. I sugget doing as much of the data retrieval as possible in the Data Access Object so that the view controllers can read the properties of the data objects without having to worry about the backend.

That's all there is to it! I hope this helped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!