Structure
Student.h
#import <Foundation/Foundation.h>
#import "sqlite3.h"
@interface Student : NSObject
@property (nonatomic, readwrite) NSInteger StudentId,
Age;
@property (nonatomic, retain) NSString *Name,
*Perc;
+ (NSMutableArray *)readRecords;
@end
Student.m
@synthesize StudentId,
Age,
Name,
Perc;
+ (NSMutableArray *)readRecords
{
NSMutableArray *recordArr = [[NSMutableArray alloc] init];
NSString *query = [NSString stringWithFormat:@"SELECT * FROM student ORDER BY Name ASC"];
sqlite3 *db;
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *dbPath = [documentsDir stringByAppendingPathComponent:@"test.sqlite"];
if(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK)
{
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(db,[query UTF8String], -1, &compiledStatement, NULL) == SQLITE_OK)
{
while(sqlite3_step(compiledStatement) == SQLITE_ROW)
{
Student *currentSObj = [[Student alloc] init];
currentSObj.StudentId = sqlite3_column_int(compiledStatement, 0);
currentSObj.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)];
currentSObj.Age = sqlite3_column_int(compiledStatement, 2);
currentSObj.Perc = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)];
[recordArr addObject:currentSObj];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(db);
return recordArr;
}
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self checkDatabase];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ip22ViewController alloc] initWithNibName:@"ip22ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)checkDatabase
{
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"test.sqlite"];
NSString *databaseName = @"test.sqlite";
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:databasePath])
{
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager removeItemAtPath:databasePath error:nil];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
else
{
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
}
ViewController.h
@interface ip22ViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
NSMutableArray *studentRecordsArr;
}
ViewController.m
#import "Student.h"
#import "StudentCellViewController.h"
- (void)viewDidLoad
{
[super viewDidLoad];
studentRecordsArr = [Student readRecords];
}
#pragma mark - Table Methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 100;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return studentRecordsArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
Student *sObj = [studentRecordsArr objectAtIndex:indexPath.row];
StudentCellViewController *sVC = [[StudentCellViewController alloc] init];
sVC.studentObj = sObj;
[cell.contentView addSubview:sVC.view];
return cell;
}
StudentCellViewController.h
@class Student;
@interface StudentCellViewController : UIViewController
@property (nonatomic, retain) IBOutlet UILabel *NameLbl,
*AgeLbl,
*PercLbl;
@property (nonatomic, retain) Student *studentObj;
StudentCellViewController.m
#import "Student.h"
@synthesize NameLbl,
AgeLbl,
PercLbl;
@synthesize studentObj;
- (void)viewDidLoad
{
[super viewDidLoad];
NameLbl.text = studentObj.Name;
AgeLbl.text = [NSString stringWithFormat:@"%dyr", studentObj.Age];
PercLbl.text = [NSString stringWithFormat:@"%.2f%%", studentObj.Perc.doubleValue];
}
No comments:
Post a Comment
Comment