Database Integration :
1) Create new Project
2) Add libsqlite3.0.dylib, UIKit, Foundation, Coregraphics Frameworks
3) Set -fno-objc-arc flag to FMDatabase.m, FMDatabaseAdditions.m, FMResultSet.m
4) Drag and droop SQLite File to the project
5) Crate Constant.h file for setting constant fixed value ex like db name, messages etc.
// Constant.h
#import "SharedDataManager.h"
#import "AppDelegate.h"
#define HEADER_FONT [UIFont fontWithName:@"HelveticaNeue" size:18]
#define kDATA_STORAGE_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
#define kDB_NAME @“dbname.sqlite"
#define kDB_PATH [kDATA_STORAGE_PATH stringByAppendingPathComponent:kDB_NAME]
#define kIMG_PATH [kDATA_STORAGE_PATH stringByAppendingPathComponent:@"Images"]
#define kSHARED_INSTANCE [SharedDataManager sharedDbManager]
#define kDATABASE [kSHARED_INSTANCE database]
#define kAPP_DELEGATE (AppDelegate *)[[UIApplication sharedApplication] delegate]
// SharedDataManager.h
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "FMDatabase.h"
@interface SharedDataManager : NSObject {
FMDatabase *database;
}
+ (SharedDataManager *)sharedDbManager;
@property (nonatomic, retain) FMDatabase *database;
@end
// SharedDataManager.m
#import "SharedDataManager.h"
@implementation SharedDataManager
@synthesize database;
+ (SharedDataManager *)sharedDbManager {
static SharedDataManager *sharedDbManager;
@synchronized(self) {
if(!sharedDbManager) {
sharedDbManager = [[SharedDataManager alloc] init];
}
}
return sharedDbManager;
}
- (id)init {
self = [super init];
if (self != nil)
{
if(!self.database) {
self.database = [FMDatabase databaseWithPath:kDB_PATH];
}
}
[self copyDatabaseToPath];
[self openDb];
return self;
}
-(void) copyDatabaseToPath {
NSFileManager *fileManager = [NSFileManager defaultManager] ;
if(![fileManager fileExistsAtPath:kDB_PATH]) {
NSString *fromPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kDB_NAME];
[fileManager copyItemAtPath:fromPath toPath:kDB_PATH error:nil];
}
}
-(void) openDb {
if (![self.database open]) {
NSLog(@"Could not open db.");
}
}
// in pch #import "Constant.h"
// Basic operations
- (IBAction)addData:(id)sender
{
NSString *query = [NSString stringWithFormat:@"INSERT INTO STUDENT (STUDENTid,NAME) VALUES (%@,'%@')",self.textViewNumber.text,self.textViewName.text];
[kDATABASE executeUpdate:query];
self.textViewNumber.text = self.textViewName.text = @"";
}
- (IBAction)showData:(id)sender
{
[names removeAllObjects];
NSString *selectQuery = @"SELECT * FROM STUDENT";
FMResultSet *rs = [kDATABASE executeQuery:selectQuery];
while ([rs next])
{
NSLog(@"data :- %d %@",[rs intForColumn:@"STUDENTid"],[rs stringForColumn:@"NAME"]);
[names addObject:[rs stringForColumn:@"NAME"]];
}
[self.myTableView reloadData];
}
- (IBAction)searchAction:(id)sender
{
NSString *keyWord = [self.textViewSearchName text];
NSString *selectQuery = @"SELECT * FROM STUDENT";
FMResultSet *rs = [kDATABASE executeQuery:selectQuery];
while ([rs next])
{
NSString *selectedName = [rs stringForColumn:@"NAME"];
if ([keyWord isEqualToString:selectedName])
{
toggleFound = 1;
break;
}
else
{
toggleFound = 0;
}
}
toggleFound?(self.labelStaus.text = @"Found"):(self.labelStaus.text = @"Not Found");
}
- (IBAction)deleteAction:(id)sender
{
NSString *keyWord = [self.textViewDeleteName text];
NSString *deleteQuery = [NSString stringWithFormat:@"DELETE * FROM STUDENT WHERE NAME = '%@'",keyWord];
NSLog(@"Delete Query = %@",deleteQuery);
[kDATABASE executeQuery:deleteQuery];
[self.myTableView reloadData];
}
No comments:
Post a Comment