gpt4 book ai didi

iphone - iOS 为 SQLite 记录添加书签时出现问题

转载 作者:搜寻专家 更新时间:2023-10-30 20:20:35 25 4
gpt4 key购买 nike

我已经创建了一个包含多个数据库的字典应用程序,除了在书签中保存列外,一切正常! ,我知道,不可能更改 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];
}

最佳答案

I know , it's not possible change files in NSBundle

太好了,至少你努力用谷歌搜索了这个问题。那么,为什么不在应用程序首次启动时将这些要更新的文件复制到某个可写路径,例如 NSDocumentsDirectory?

NSArray *paths = NSSearchPathsForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES);
NSString *docsDir = [paths objectAtIndex:0];
[[NSFileManager defaultManager] copyItemAtPath:path toPath:[docsDir stringByAppendingPathComponent:@"db.sql"] error:NULL];

等等

关于iphone - iOS 为 SQLite 记录添加书签时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12545765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com