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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With