1. First we have to configure database in iPhone
MCDatabase.m File
MCDatabase.h File
#import "Foundation/Foundation.h"
#import "/usr/include/sqlite3.h"
#define kDATABASE_NAME @"eCommerce.sqlite3"
@interface MCDatabase : NSObject
{
}
+(MCDatabase *)getSharedInstance;
- (sqlite3 *)getOpenedDatabase;
- (BOOL)closeDatabase;
- (BOOL)isDatabaseOpened;
@end
#import "MCDatabase.h"
static sqlite3 *database;
static BOOL opened;
static MCDatabase *sharedDatabase
= nil;
@implementation MCDatabase
//Create
shared object for the 'Database'
+ (MCDatabase *)getSharedInstance
{
@synchronized(self) {
if (sharedDatabase == nil)
sharedDatabase = [[MCDatabase alloc] init];
}
return sharedDatabase;
}
- (sqlite3 *)getOpenedDatabase
{
@try {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths
objectAtIndex:0];
NSString *databasePath =
[documentsDirectory stringByAppendingPathComponent:kDATABASE_NAME];
int result = sqlite3_open([databasePath
UTF8String],
&database);
if (result == SQLITE_OK) {
opened = YES;
}else {
sqlite3_close(database);
}
}
@catch (NSException * e) {
NSLog(@"eCommerce MCDatabaseConfig-getDatabase: Caught %@:
%@", [e name], [e reason]);
}
return database;
}
- (BOOL)closeDatabase
{
@try {
sqlite3_close(database);
opened = NO;
}
@catch (NSException * e) {
NSLog(@"eCommerce MCDatabaseConfig-closeDatabase: Caught %@:
%@", [e name], [e reason]);
}
return !opened;
}
- (BOOL)isDatabaseOpened
{
return opened;
}
- (void)dealloc
{
free(database);
[super dealloc];
}
@end