我创建了一个多语言词典应用程序,它读取了几个数据库,我有一个书店的问题...在应用程序的MainViewController中,我有一个用户选择词典的动作菜单。所以我为这些词典创建了一个键来识别选择了哪个词典。这是我的代码:
阅读数据库
- (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;
}
问题是我无法在设备上添加任何单词!有什么问题???
这里是字典更改功能:
- (void)eng_pr
{
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInt:1] forKey:@"Bv"];
[[NSUserDefaults standardUserDefaults] synchronize];
dbClass.viewController = self;
self.searchbar.placeholder = @"Search";
[myTable reloadData];
}
和书签:
-(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)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];
}
答案 0 :(得分:3)
好的,问题是您的数据库(.sqlite)位于资源文件夹中,资源文件夹中的所有数据/文件都可以读取但无法写入。 当你调用书签方法时,你做一个查询(UPDATE ...),所以它尝试修改sqlite中的数据,但它不能,因为你的.sqlite在资源文件夹中。
您的问题的解决方案是将.sqlite文件放在Document目录中,而不是放在资源目录中;)
编辑:如果我是你,在第一个应用程序启动时,将所有.sqlite从Resources文件夹复制到Document文件夹,并使用位于Document文件夹中的.sqlite。 / p>
一些有用的提示:Copy file from Resources folder to Document folder
编辑2:
执行此操作时:
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;
}
您正在从Resource文件夹中读取文件,您应该从docuemnt目录中读取sqlite:
switch (kValue) {
case 1:
documentsDir = [NSString stringWithFormat:@"%@/eg-pr.sqlite", documentsDir];
break;
case 2:
documentsDir = [NSString stringWithFormat:@"%@/pr-eng.sqlite", documentsDir];
default:
break;
}
此外,如果kValue不等于1或2,则documentDir等于[paths objectAtIndex:0]; (因此它将返回Document目录本身而不是文档目录中的sqlite文件)它应该是这样的吗? :
documentsDir = [NSString stringWithFormat:@"%@/filename.sqlite", documentsDir];