iOS :Issue with bookmarking SQL database - iphone

I have created a multi lingual dictionary app which reads several databases , I have a problem with bookmaring... In the app's MainViewController I have an action menu which users select dictionaries. so I have created a key for these dictionaries to recognizing which dictionary is selected . Here is my code :
Reading Data Base
- (NSString *) getDBPath
{
NSInteger kValue = [[[NSUserDefaults standardUserDefaults] stringForKey:#"Bv"] intValue];
NSString *documentsDir = [[NSString alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
documentsDir = [paths objectAtIndex:0];
switch (kValue) {
case 1:
documentsDir = [[NSBundle mainBundle]pathForResource:#"eg-pr" ofType:#"sqlite"];
break;
case 2:
documentsDir = [[NSBundle mainBundle]pathForResource:#"pr-eng" ofType:#"sqlite"];
default:
break;
}
return documentsDir;
}
the problem is I can not bookmark any word on the device !! what is the problem ???
here is dictionary changing function :
- (void)eng_pr
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:1] forKey:#"Bv"];
[[NSUserDefaults standardUserDefaults] synchronize];
dbClass.viewController = self;
self.searchbar.placeholder = #"Search";
[myTable reloadData];
}
and bookmarking :
-(void) setBookMark:(NSInteger)oid {
sqlite3_stmt *statement;
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *SetFavSQL = [NSString stringWithFormat: #"UPDATE DIC SET bookmark=1 WHERE id=\"%d\"", oid];
// NSLog(#"%#",SetFavSQL);
const char *SetFav_stmt = [SetFavSQL UTF8String];
sqlite3_prepare_v2(database, SetFav_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) != SQLITE_DONE)
{
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
EDITED :
SEARCHING FUNCTION
- (void)searchWord:(NSString *)txt{
NSMutableArray *DB_Array = [[NSMutableArray alloc] init];
NSString *dbPath = [[NSString alloc] initWithString: [self getDBPath]];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
NSString *sql =[NSString stringWithFormat:#"SELECT * FROM DIC Where Name LIKE \'%#%%\' order by NAME LIMIT 30",txt];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSInteger oid = sqlite3_column_int(compiledStatement, 0);
const char* f1 = (const char*)sqlite3_column_text(compiledStatement, 1);
NSString *oName = f1 == NULL ? nil : [[NSString alloc] initWithUTF8String:f1];
const char* f2 = (const char*)sqlite3_column_text(compiledStatement, 2);
NSString *oMean = f2 == NULL ? nil : [[NSString alloc] initWithUTF8String:f2];
const char* f3 = (const char*)sqlite3_column_text(compiledStatement, 3);
NSString *oPron = f3 == NULL ? nil : [[NSString alloc] initWithUTF8String:f3];
NSInteger bm = sqlite3_column_int(compiledStatement, 5);
readerClass = [[Reader alloc]initWithReadDB:oid Name:oName Mean:oMean Pron:oPron bookMark:bm];
[DB_Array addObject:readerClass];
}
}
else {
NSLog(#"Error retrieving data from database.");
}
sqlite3_close(database);
}
else {
NSLog(#"Error: Can't open database!");
}
AppDelegate *appDelegateClass = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegateClass.wordList removeAllObjects];
[appDelegateClass.wordList=DB_Array mutableCopy];
}

OK, the problem is that your database (.sqlite) are located in the Resources Folder and all data/files int the resources folder can be read but cannot be written.
And when you call the bookmark method your do a query (UPDATE ...) so it try to modify data in the sqlite and it just can't because your .sqlite is in the Resources Folder.
The solution to your problem is to put your .sqlite file in the Document directory and not in the resource directory ;)
Edit : If I were you, at the first application start, copy all your .sqlite from the Resources folder to the Document folder, and work with the .sqlite located in the Document folder.
Some useful tips : Copy file from Resources folder to Document folder
Edit 2:
When you do this :
switch (kValue) {
case 1:
documentsDir = [[NSBundle mainBundle]pathForResource:#"eg-pr" ofType:#"sqlite"];
break;
case 2:
documentsDir = [[NSBundle mainBundle]pathForResource:#"pr-eng" ofType:#"sqlite"];
default:
break;
}
You are reading the files from the Resource folder, you should read the sqlite from the docuemnt directory :
switch (kValue) {
case 1:
documentsDir = [NSString stringWithFormat:#"%#/eg-pr.sqlite", documentsDir];
break;
case 2:
documentsDir = [NSString stringWithFormat:#"%#/pr-eng.sqlite", documentsDir];
default:
break;
}
Moreover if kValue is not equals to 1 or 2 the documentDir equals [paths objectAtIndex:0]; (so it will return the Document directory itself and not the sqlite files located in the document directory) does it should be something like that ? :
documentsDir = [NSString stringWithFormat:#"%#/filename.sqlite", documentsDir];

Related

sqlite3_prepare_v2 doesn't return sqlite_ok on an iPhone

I am trying out SQLite in iPhone for the very first time. The error I am facing is that the statement sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK in th ecode below is returning false and nothing is displayed.
Below is my code:
+ (void) getInitialDataToDisplay:(NSString *)dbPath {
dbTryAppDelegate *appDelegate = (dbTryAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select name,id from studtry";
sqlite3_stmt *selectstmt;
//below line is never executed and its else part is also not executed.
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
NSInteger primaryKey = sqlite3_column_int(selectstmt, 0);
stud *coffeeObj = [[stud alloc] initWithPrimaryKey:primaryKey];
coffeeObj.studName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
//coffeeObj.isDirty = NO;
[appDelegate.studArray addObject:coffeeObj];
// [coffeeObj release];
}
}
}
else
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
}
Where am I getting wrong? How do I solve it?
Check Database Path, Database extension and also same table & fields.
Anyways,
Use This instead of using directly const string::
NSString *sqlStr = [NSString stringWithFormat:#"select name,id from studtry"];
const char *sql = [sqlStr UTF8String];
After while loop finalize statement,
sqlite3_finalize(selectstmt);
Then, after If condition,
sqlite3_close(database);
No need to write in else part.
Hopefully, it'll work for you.
Thanks.
First delete your application from Application & Simulator Folder.
Then create Database named :: Practise.sqlite3 (Type of SQLite3)
Then write this methods for Copying:
- (void) copyDatabaseIfNeeded {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"Practise.sqlite3"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"Practise.sqlite3"];
}
You can copy this whenever you want by,
[self copyDatabaseIfNeeded];
Then, check for your results. Hopefully it'll work.
As per your code, all thing is abs right but I think you are passing the wrong DBPath or maybe DB file is not on the right place
please check this both point and try again.

iOS Having issue with bookmarking SQLite record

I have created a dictionary application with several data bases , everything works fine except saving columns in bookmark ! , I know , it's not possible change files in NSBundle but I don't know how can I fix it . I would be grateful if you help me out here is the code :
- (NSString *) getDBPath {
NSString *path;
if ( [[[NSUserDefaults standardUserDefaults] stringForKey:#"Bv"] intValue] == 2)
{
path = [[NSBundle mainBundle]pathForResource:#"pr-eng" ofType:#"sqlite"];
} else {
path = [[NSBundle mainBundle]pathForResource:#"eg-pr" ofType:#"sqlite"];
}
if ( [[[NSUserDefaults standardUserDefaults] stringForKey:#"Bv"] intValue] == 3)
{
path = [[NSBundle mainBundle]pathForResource:#"german" ofType:#"sqlite"];
}
return path;
}
Here is bookmarking function :
-(void) setBookMark:(NSInteger)oid {
sqlite3_stmt *statement;
const char *dbpath = [[self getDBPath] UTF8String];
if (sqlite3_open(dbpath, &database) == SQLITE_OK)
{
NSString *SetFavSQL = [NSString stringWithFormat: #"UPDATE DIC SET bookmark=1 WHERE id=\"%d\"", oid];
// NSLog(#"%#",SetFavSQL);
const char *SetFav_stmt = [SetFavSQL UTF8String];
sqlite3_prepare_v2(database, SetFav_stmt, -1, &statement, NULL);
if (sqlite3_step(statement) != SQLITE_DONE)
{
}
sqlite3_finalize(statement);
sqlite3_close(database);
}
}
Reading database :
-(void) read_Database{
NSMutableArray *DB_Array = [[NSMutableArray alloc] init];
NSMutableArray *word_Array = [[NSMutableArray alloc] init];
if(sqlite3_open([[self getDBPath]UTF8String], &database) == SQLITE_OK) {
NSString *sql =#"SELECT * FROM DIC";
// NSLog(#"%#",sql);
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(database, [sql UTF8String] , -1, &compiledStatement, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
NSInteger oid = sqlite3_column_int(compiledStatement, 0);
const char* f1 = (const char*)sqlite3_column_text(compiledStatement, 1);
NSString *oName = f1 == NULL ? nil : [[NSString alloc] initWithUTF8String:f1];
const char* f2 = (const char*)sqlite3_column_text(compiledStatement, 2);
NSString *oMean = f2 == NULL ? nil : [[NSString alloc] initWithUTF8String:f2];
const char* f3 = (const char*)sqlite3_column_text(compiledStatement, 3);
NSString *oPron = f3 == NULL ? nil : [[NSString alloc] initWithUTF8String:f3];
NSInteger bm = sqlite3_column_int(compiledStatement, 5);
readerClass = [[Reader alloc]initWithReadDB:oid Name:oName Mean:oMean Pron:oPron bookMark:bm];
[DB_Array addObject:readerClass];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(database);
AppDelegate *appDelegateClass = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegateClass.wordList removeAllObjects];
appDelegateClass.wordList=[word_Array mutableCopy];
[appDelegateClass.dictionaryArray removeAllObjects];
appDelegateClass.dictionaryArray=[DB_Array mutableCopy];
}
I know , it's not possible change files in NSBundle
Great, at least you made the effort to google the issue. Then, why not just copy these files to be updated upon the very first launch of the application to some writable path, like NSDocumentsDirectory?
NSArray *paths = NSSearchPathsForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:path toPath:[docsDir stringByAppendingPathComponent:#"db.sql"] error:NULL];
etc.
For further improvement in #H2CO3's answer and for more clarification I post my answer :)
Here is what I do to save data to database on iPhone or iPad (iOS). All data is been kept in device locally. So if app is uninstalled all data will lost, if user hasn't take backup of the app and restored if from backup.
NSFileManager *fileMgr;
NSString *homeDir;
These are the ivar I use in below functions all functions are in my .m file which is contains all my db related functions like update, insert, select, delete.
-(void)CopyDbToDocumentsFolder
{
NSError *err=nil;
fileMgr = [NSFileManager defaultManager];
NSString *dbpath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"res_in_mil.sqlite"];
NSString *copydbpath = [self.GetDocumentDirectory stringByAppendingPathComponent:#"res_in_mil.sqlite"];
[fileMgr removeItemAtPath:copydbpath error:&err];
if(![fileMgr copyItemAtPath:dbpath toPath:copydbpath error:&err])
{
UIAlertView *tellErr = [[UIAlertView alloc] initWithTitle:title message:#"Unable to copy database." delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[tellErr show];
}
}
-(NSString *) GetDocumentDirectory
{
fileMgr = [NSFileManager defaultManager];
homeDir = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"];
return homeDir;
}
As the name is self-explains there is no need to explain what these functions are for.
And in appDelegate.m I use the below code to copy my DB to the specified location where I can edit my DB. This code is in - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method.
NSFileManager *fileMgr;
myDB *dbObj = [[myDB alloc]init];
fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[dbObj GetDocumentDirectory] stringByAppendingPathComponent:#"res_in_mil.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
[dbObj CopyDbToDocumentsFolder];
I use this for one DB as I have only one DB. But I think you can extend these functions to copy more then one DB.
Even you can make one function to identify that the required DB is in DocumentDirectory or not using the code of - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions method given above.
And for insert, update, delete, select operation I use the below code to open the DB for any access.
fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[self GetDocumentDirectory] stringByAppendingPathComponent:#"res_in_mil.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(#"Cannot locate database file '%#'",dbPath);
[self CopyDbToDocumentsFolder];
}
Happy Coding :)
I think you have to make only one master database and can have multiple tables in it. That can resolve your problem.

How to return sqlite3_stmt to called object

I am working on an iPhone app. I have created a re usable class in which a sqlite getData method is written. I want to pass a sql statement from my controller and want to get an array back with all of the rows.
Can I get sqlite3_stmt object stored into array and return that array, so at calling point I can cast it and find out each columns value?
My current code is like that :
-(NSMutableArray*)getData:(NSString*) SqlQuery
{
// The Database is stoed in the application bundle
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"sqliteClasses.sqlite"];
if(sqlite3_open([path UTF8String], &contactDB) == SQLITE_OK)
{
const char *sql = (const char*)[SqlQuery UTF8String];
sqlite3_stmt *compiledStatement;
if(sqlite3_prepare_v2(contactDB, sql, -1, &compiledStatement, NULL) == SQLITE_OK)
{
// Loop through the results and add them to the feeds array
while (sqlite3_step(compiledStatement) == SQLITE_ROW)//(stepResult == SQLITE_ROW)
{
// [arrayRecords addObject:compiledStatement];
}
}
sqlite3_finalize(compiledStatement);
}
sqlite3_close(contactDB);
return arrayRecords;
}
The error line is : [arrayRecords addObject:compiledStatement];
How Can I achieve this ? any alternate for implementing this ?
Thanks.
- (NSArray *) getActionWithFilters:(NSDictionary *)dictionary ClassName:(NSString *)className Error:(NSError **)error{
NSString *query = [NSString stringWithFormat:#"SELECT * FROM %# %#",className,[self getFitlerArrayByDictionary:dictionary]];
sqlite3_stmt *statement = [self getItemsWithQuery:query];
NSMutableArray *array = [[NSMutableArray alloc] init];
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableDictionary *itemDic = [[NSMutableDictionary alloc] init];
int columns = sqlite3_column_count(statement);
for (int i=0; i<columns; i++) {
char *name = (char *)sqlite3_column_name(statement, i);
NSString *key = [NSString stringWithUTF8String:name];
switch (sqlite3_column_type(statement, i)) {
case SQLITE_INTEGER:{
int num = sqlite3_column_int(statement, i);
[itemDic setValue:[NSNumber numberWithInt:num] forKey:key];
}
break;
case SQLITE_FLOAT:{
float num = sqlite3_column_double(statement, i);
[itemDic setValue:[NSNumber numberWithFloat:num] forKey:key];
}
break;
case SQLITE3_TEXT:{
char *text = (char *)sqlite3_column_text(statement, i);
[itemDic setValue:[NSString stringWithUTF8String:text] forKey:key];
}
break;
case SQLITE_BLOB:{
//Need to implement
[itemDic setValue:#"binary" forKey:key];
}
break;
case SQLITE_NULL:{
[itemDic setValue:[NSNull null] forKey:key];
}
default:
break;
}
}
[array addObject:itemDic];
[itemDic release];
}
return [array autorelease];
}

SQLite issue: sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) not working

-(void)myDatabaseFunction
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:#"HHAuditToolDatabase.sqlite"];
if (sqlite3_open([writableDBPath UTF8String], &database) == SQLITE_OK){
NSLog(#"opening db");
NSString *keyValue;
NSString *sqlStr = #"SELECT * FROM HHAuditTable";
//following if() not working dude!!!!
//its working with !=SQLITE_OK
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
sqlite3_stmt *addStmt = nil;
if(sqlite3_prepare_v2(database,[sqlStr UTF8String], -1, &addStmt, NULL) != SQLITE_OK){
NSLog(#"%#",sqlStr);
while (sqlite3_step(addStmt) == SQLITE_ROW) {
const unsigned char *querry_returns = sqlite3_column_text(addStmt, 0);
keyValue = [[NSString alloc]initWithUTF8String:(char *) sqlite3_column_text(addStmt, 0)];
}
NSLog(#"value from DB = %#",keyValue);
That if() with comment doesn't work....Some have a cure!!! i have been on it for last 3 hrs....please come up with a soln
You are opening the database twice. You should have to close the database connection and then you have to open the database again. That is why it is not working.
Hay you can use my code its working fine for me:-
(void)myDatabaseFunction {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"flipsy.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, #"Failed to create writable database file with message '%#'.", [error localizedDescription]);
}
NSString *sql = #"SELECT * FROM HHAuditTable";
NSInteger *intvalue;
result = [[NSMutableArray alloc] init];
// NSLog(#"sql--------->%#",sql);
const char *sqlStatement = [sql UTF8String];
//if (sqlite3_open([dbPath UTF8String], &database) == SQLITE_OK) {
if(sqlite3_prepare_v2(database, sqlStatement, -1, &selectstmt , NULL)!= SQLITE_OK) {
}
else {
//NSLog(#"%#",dataTypeArray);
for(int i=0;i<[dataTypeArray count];i++) {
temp = [[NSMutableArray alloc] init];
[result addObject:temp];
// [temp release];
}
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
for(int i=0;i<[dataTypeArray count];i++) {
switch( [[dataTypeArray objectAtIndex:i] integerValue] ) {
case 0:
intvalue = (NSInteger *)sqlite3_column_int(selectstmt,i);
strvalue = [NSString stringWithFormat:#"%d",intvalue];
[[result objectAtIndex:i] addObject:(NSNumber *)strvalue];
break;
case 1:
[[result objectAtIndex:i] addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, i)]];
break;
case 2:
blob = [[NSData alloc] initWithBytes:sqlite3_column_blob(selectstmt, i) length:sqlite3_column_bytes(selectstmt, i)];
[[result objectAtIndex:i] addObject:blob];
[blob autorelease];
break;
default:
defaultValue=[NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, i)];
break;
}//switch
}//for(int i=0;i<[dataTypeArray count];i++)
}//while(sqlite3_step(selectstmt) == SQLITE_ROW)
//}//else
//sqlite3_close(database);
sqlite3_finalize(selectstmt);
[temp release];
}//if (sqlite3_open([dbPath UTF
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:#"flipsy.sqlite"];
}
Please let me know if any clarification needed

making map coordinates(lan,& long)storing in sqlite database

how to created an application that records a series of longitude and latitude values in a SQLite database and display them as a coloured track on a MapActivity.
I need help. How can store the map coordinates in sqlite database and display like journey details in table .For example I have done one journey from mumbai to Pune .Then how can store the data into database that can available for future reference .when user click on journey name it should give all details
If you are new to Sqlite Then look into this class for data base
Create two database file as the following
---->>>>
Database.h
Write the following code in this file
#import <Foundation/Foundation.h>
#import <sqlite3.h>
#interface DataBase : NSObject {
sqlite3 *database;
}
+(DataBase *) shareDataBase;
-(BOOL) createDataBase:(NSString *)DataBaseName;
-(NSString*) GetDatabasePath:(NSString *)database;
-(NSMutableArray *) getAllDataForQuery:(NSString *)sql forDatabase:(NSString *)database;
-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1;
-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1;
-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1;
#end
---->>>>
Database.m
Write the following code in this file
#import "DataBase.h"
#implementation DataBase
static DataBase *SampleDataBase =nil;
+(DataBase*) shareDataBase{
if(!SampleDataBase){
SampleDataBase = [[DataBase alloc] init];
}
return SampleDataBase;
}
-(NSString *) GetDatabasePath:(NSString *)database1{
[self createDataBase:database1];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:database1];
}
-(BOOL) createDataBase:(NSString *)DataBaseName{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DataBaseName];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return success;
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:DataBaseName];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error!!!" message:#"Failed to create writable database" delegate:self cancelButtonTitle:#"Cancel" otherButtonTitles:nil];
[alert show];
[alert release];
}
return success;
}
-(NSMutableArray *) getAllDataForQuery:(NSString *)sql forDatabase:(NSString *)database1{
sqlite3_stmt *statement = nil ;
NSString *path = [self GetDatabasePath:database1];
NSMutableArray *alldata;
alldata = [[NSMutableArray alloc] init];
if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
{
NSString *query = sql;
if((sqlite3_prepare_v2(database,[query UTF8String],-1, &statement, NULL)) == SQLITE_OK)
{
while(sqlite3_step(statement) == SQLITE_ROW)
{
NSMutableDictionary *currentRow = [[NSMutableDictionary alloc] init];
int count = sqlite3_column_count(statement);
for (int i=0; i < count; i++) {
char *name = (char*) sqlite3_column_name(statement, i);
char *data = (char*) sqlite3_column_text(statement, i);
NSString *columnData;
NSString *columnName = [NSString stringWithCString:name encoding:NSUTF8StringEncoding];
if(data != nil)
columnData = [NSString stringWithCString:data encoding:NSUTF8StringEncoding];
else {
columnData = #"";
}
[currentRow setObject:columnData forKey:columnName];
}
[alldata addObject:currentRow];
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
return alldata;
}
-(void) inseryQuery:(NSString *) insertSql forDatabase:(NSString *)database1{
sqlite3_stmt *statement = nil ;
NSString *path = [self GetDatabasePath:database1];
if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
{
if((sqlite3_prepare_v2(database,[insertSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_OK){
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
-(void) updateQuery:(NSString *) updateSql forDatabase:(NSString *)database1{
sqlite3_stmt *statement = nil ;
NSString *path = [self GetDatabasePath:database1];
if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
{
if((sqlite3_prepare_v2(database,[updateSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_OK){
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
-(void) deleteQuery:(NSString *) deleteSql forDatabase:(NSString *)database1{
sqlite3_stmt *statement = nil ;
NSString *path = [self GetDatabasePath:database1];
if(sqlite3_open([path UTF8String],&database) == SQLITE_OK )
{
if((sqlite3_prepare_v2(database,[deleteSql UTF8String],-1, &statement, NULL)) == SQLITE_OK)
{
if(sqlite3_step(statement) == SQLITE_OK){
}
}
sqlite3_finalize(statement);
}
sqlite3_close(database);
}
#end
Now to get data use the following code
NSString *sql = #"select * from UserInfo"; <br>
userInfo = [[DataBase shareDataBase] getAllDataForQuery:sql forDatabase:#"Sample.db"];
It will return array of all the row in form of NSDictionary.
To add new record use the following code
NSString *sql = [NSString stringWithFormat:#"insert into userInfo values ('city','name','phone')"];
[[DataBase shareDataBase] inseryQuery:sql forDatabase:#"Sample.db"];
In the same way there is also method to update and delete record.
so This is the best example I have seen we just need to call one method to for fetch, insert , update or delete.
Thanks for seeing the question,
To get location import corelocation framework in your project.
follow this link
http://developer.apple.com/library/ios/#samplecode/LocateMe/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007801 to get sample for getting location.
This is the format to set location in json
[{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude":"45.2655","Latitude":"23.2655"},{"Longitude":"45.2655","Latitude":"23.2655"}]
Thanks.
You need to create a table with fields ...... Source,Destination,SourceLat,SourceLong,DestinationLat,DestinationLong....... and in this you will pass
Source - Mumbai,(or other) - text - varchar type
Destinatino - Pune, (or other) -text - varchar type
SourceLat - coordinate.latitude; - number with decimal precison upto 10 points.
SourceLong - coordinate.longitude - number with decimal precison upto 10 points.
DestinationLat - coordinate.latitude; - number with decimal precison upto 10 points.
DestinationLong - coordinate.longitude - number with decimal precison upto 10 points.
Thanks,
First you need to create object of NSMutableArray *arrayOflocation; in .h file,
Then in you locationUpdate method write the following code
NSMutableDictionary *LocationDic = [[NSMutableDictionary alloc] init];
[LocationDic setObject:[NSString stringWithFormat:#"%f",c.latitude] forKey:#"Latitude"];
[LocationDic setObject:[NSString stringWithFormat:#"%f",c.longitude] forKey:#"Longitude"];
[arrayOflocation addObject:LocationDic];
now when you save the trip you need to create the string for the json format for that you need to use json API you can get it using google easily . write the following code when you want to save the string in file.
NSString *dataString = [arrayOflocation JSONRepresentation];
//// code to write dataString in txt file.
and at finally need to store the file name in sqlite along with other detail for the trip.