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:-
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)