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];
}
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.
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.
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 *)launchOptionsmethod.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 *)launchOptionsmethod given above.And for insert, update, delete, select operation I use the below code to open the DB for any access.
Happy Coding 🙂