我创建了一个包含多个数据库的字典应用程序,除了将列保存在书签中之外,一切正常!,我知道,不可能更改 NSBundle 中的文件,但我不知道如何修复它。如果您能帮助我,我将不胜感激这里是代码:
- (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;
}
这是书签功能:
-(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);
}
}
读取数据库:
-(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];
}