We can include the database in our iPad/iPhone applications with the help of SQLite and FMDB wrapper class. Here is how we can achieve it with following simple steps.
1. First with the help of Mozilla Firefox SQLite plug-in create a MyDatabase.sqlite file and save it in your project folder.
2.Get the FMDB Wrapper classes from the git hub project https://github.com/ccgus/fmdb unzip it and navigate to the fmdb folder (fmdb-master.zip\fmdb-master\src\fmdb) and copy this to your Xcode project.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ //-------------------DataBase connection---------------------- NSFileManager *fileManager = [NSFileManager defaultManager]; NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; // To Get the path of the database file during runtime of application NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"MYDatabase.sqlite"]; NSLog(@"Database path: %@",databasePath); if ([fileManager fileExistsAtPath:databasePath] == NO) { NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"MYDatabase" ofType:@"sqlite"]; [fileManager copyItemAtPath:resourcePath toPath:databasePath error:&error]; } //save the database path in userdefaults so that you can use it for connecting to database and making database operations [[NSUserDefaults standardUserDefaults] setValue:databasePath forKey:dbPath]; [[NSUserDefaults standardUserDefaults] synchronize]; }
4. In ViewController.m file of your project import the two header files
#import "FMDatabase.h"
#import "FMResultSet.h"
then write the following code to connect to the database and make the insertion and selection operations in your app Database.
- (void)viewDidLoad { [self insertDataInDB]; //array dbResultArray Has the resultant rows retrieved from db by SELECT Query NSArray *dbResultArray=[NSArray arrayWithArray:[self getDataFromDB]] ; //To store the data retrieved in a variables for use //Use your convenient way to store the retrieved data and use it in your app for(int i=0 ; i<dbResultArray.count;i++){ NSString *str1=[[dbResultArray objectAtIndex:i] valueForKey:@"column1Value"]; NSString *str2=[[dbResultArray objectAtIndex:i] valueForKey:@"column2Value"]; . . . NSString *strN=[[dbResultArray objectAtIndex:i] valueForKey:@"columnNValue"]; }//String }
This below method inserts the data in to database :
-(void)insertDataInDB { //Connec to database NSString *dbPath=[[NSUserDefaults standardUserDefaults]valueForKey:dbPath]; FMDatabase *localDB = [FMDatabase databaseWithPath:dbPath]; [localDB open]; NSString *queryStr; queryStr=[NSString stringWithFormat:@"INSERT INTO table_name (Collumn1,Collumn2,Collumn3,Collumn4,....,CollumnN) VALUES (Value1,'Value2','Value3','Value4',.....,'ValueN')"]; if (![localDB executeUpdate:queryStr]) { //NSLog(@"failed Inserting data to DB: %@", [localDB lastErrorMessage]); } [localDB close]; }
This below method returns the array of Dictionary containing the Retrieved data from database:
-(NSMutableArray *)getDataFromDB { //Connec to database NSString *dbPath=[[NSUserDefaults standardUserDefaults]valueForKey:dbPath]; FMDatabase *localDB = [FMDatabase databaseWithPath:dbPath]; [localDB open]; NSMutableArray *tempArrayV=[[NSMutableArray alloc]init]; NSString *queryStr=[NSString stringWithFormat:@"SELECT * from table_name"]; FMResultSet *resultQuery = [localDB executeQuery:queryStr]; //After the query is executed while ([resultQuery next]) { NSDictionary * DataDict =[NSDictionary dictionaryWithObjectsAndKeys: [resultQuery stringForColumn:@"Column1"],@"column1Value", [resultQuery stringForColumn:@"Column2"],@"column2Value", [resultQuery stringForColumn:@"Column3"],@"column3Value", . . . [resultQuery stringForColumn:@"ColumnN"],@"columnNValue",nil]; [tempArrayV addObject:DataDict]; } return tempArrayV; [localDB close]; }
Hope this will help you to implement the Application with the database support.