Hi Readers,
This blog includes the concept of how to fetch data from database after saving the data. You can go through with previous blog if you are not aware about how to save data How to save data in SQLite. Now we will discuss the method to fetch data from database.
The method getAllEmployees is defined in DataBaseManager file as displayed below:-
- (NSArray*)getAllEmployees {
const char *dbpath = [self.databasePath UTF8String];
NSMutableArray *resultArray = [NSMutableArray new];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *getEmployeesSQL = [NSString stringWithFormat:@"select * from Employee"];
const char * get_stmt = [getEmployeesSQL UTF8String];
if (sqlite3_prepare_v2(database,
get_stmt, -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSInteger empId = sqlite3_column_int(statement, 0);
NSString *name = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 1)];
NSString *address = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 2)];
NSString *email = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 3)];
NSString *phone = [[NSString alloc] initWithUTF8String:
(const char *) sqlite3_column_text(statement, 4)];
EmpModel *model = [[EmpModel alloc] initWithName:name address:address phone:phone email:email empId:empId];
[resultArray addObject:model];
}
sqlite3_reset(statement);
}
else{
NSLog(@"Not found");
return nil;
}
}
NSLog(@"%@",resultArray);
return resultArray;
}
You can display the data in table view as per the code snippet below:-
RecordViewController.h
#import <UIKit/UIKit.h>
#import "SubRecordTableViewCell.h"
#import "EmpModel.h"
#import "DataBaseManager.h"
@interface RecordViewController : UIViewController<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end
RecordViewController.m
#import "RecordViewController.h"
@interface RecordViewController ()
{
NSArray * arrOfNamesAndDepartment;
NSArray * arrOfEmployeeRecordFromDatabase;
}
@end
@implementation RecordViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.estimatedRowHeight=142;
self.tableView.rowHeight=UITableViewAutomaticDimension;
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)viewWillAppear:(BOOL)animated{
arrOfEmployeeRecordFromDatabase=[[DataBaseManager sharedDatabaseManager] getAllEmployees];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return arrOfEmployeeRecordFromDatabase.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"SubTable";
SubRecordTableViewCell *cell;
cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
EmpModel *model = [arrOfEmployeeRecordFromDatabase objectAtIndex:indexPath.row];
cell.lblRecordName.text = model.empName;
cell.lblRecordAddress.text = model.empAddress;
cell.lblRecordEmail.text = model.empEmail;
cell.lblRecordPhone.text = model.empPhoneNumber;
return cell;
}
@end
Output:-
Output will be shown with the help of screenshots given below:-
![](https://lh3.googleusercontent.com/-T5ASxc1I43o/V20CkmSDpUI/AAAAAAAAFzc/AJ3pMvvP_jYh5mAomh35faNHMzcNqzIBwCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B24%252C%2B2016%252C%2B2.58.05%2BPM.png)
![](https://lh3.googleusercontent.com/-vQRgd1R9Am8/V20Dyh7dHTI/AAAAAAAAF0I/-J5dYGE_Ll4Sj0qzQOdEzrtr1-MpIzQeQCL0B/w351-h624-no/Simulator%2BScreen%2BShot%2BJun%2B24%252C%2B2016%252C%2B3.25.54%2BPM.png)
You can also download the whole project of save and fetch data from database with the help of project link given below:-
0 Comment(s)