This Post is in under Development, it will be final soon....................
This post is the basic SQLite integration in iOS project for newcomers, if you are experienced developer then it will not be helpful for you :
To create database and manage tables you can add SQLite Manager Add-Ons in Firefox please follow the below guideline:
Open SQLite Manager from Firefox:
Creating new Database:
Creating a Table :
Optional:
For SQLite DB this tool is also very helpful, if you wish to use just open sqlite file with Navicate:
Drag your SQLite database file into your project:
Make sure create group for any added folder is selected and add to target your project as show in belowimage.
Copy Database into Document Directory:
You can call this method in didFinishLaunchingWithOptions in Appdelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//Copy Database into Document Directory
[self copyDB];
}
-(void)copyDB
{
//sample is the name of your database
NSString *str = [[NSBundle mainBundle]pathForResource:@"sample" ofType:@"sqlite"];
NSString *docpath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *destPaht = [docpath stringByAppendingPathComponent:@"sample.sqlite"];
NSLog(@"databse path %@",destPaht);
NSFileManager *mgr = [NSFileManager defaultManager];
if (![mgr fileExistsAtPath:destPaht])
{
[mgr copyItemAtPath:str toPath:destPaht error:nil];
}
}
//Below is class method for Select Query to get all records from table:
+(NSMutableArray *)GetAllRecords
{
NSMutableArray *finalempArr =[NSMutableArray array];
sqlite3 *database;
if (sqlite3_open([Database_Path UTF8String], &database) == SQLITE_OK)
{
NSString *selectSql;
selectSql = [NSString stringWithFormat:@"SELECT * FROM records"];
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [selectSql cStringUsingEncoding:NSUTF8StringEncoding], -1, &statement, NULL) == SQLITE_OK)
{
while (sqlite3_step(statement) == SQLITE_ROW)
{
NSMutableDictionary *empDic =[NSMutableDictionary dictionary];
//here we get all column entries from table
NSString *emp_id = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,0)];
NSString *emp_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,1)];
NSString *emp_age = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,2)];
NSString *emp_address = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,3)];
NSString *emp_cell = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement,4)];
//adding entries in dictionary optioanl
[empDic setValue:emp_id forKey:@"id"];
[empDic setValue:emp_name forKey:@"name"];
[empDic setValue:emp_age forKey:@"age"];
[empDic setValue:emp_address forKey:@"address"];
[empDic setValue:emp_cell forKey:@"cell"];
[finalempArr addObject:empDic];
empDic=nil;
}
sqlite3_finalize(statement);
}
else
{
NSLog(@"Error");
}
sqlite3_close(database);
database=nil;
}
else
{
NSLog(@"Database cant open");
}
return finalempArr;
}
You can call above method like this:
[DatabaseClass GetAllRecords];
You can download Sample SQLite Integration Code from here.