是否将SQLite数据库从bundle复制到要写入的文档



因此,我正试图将当前位于主捆绑包中的数据库复制到文档中,这样我就可以向其写入,而不仅仅是从中读取。现在我有以下情况,但它就是找不到数据库NSLog("Found database");没有显示,因此复制不起作用。我已经做了一段时间了,但是我就是想不通。如有任何帮助,我们将不胜感激。

更新版本

- (IBAction)update:(id)sender {
@try {
/*
NSString *docsPath = [self applicationDocumentsDirectory];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
*/
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* dbPath = [documentsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
if (!fileExists) {
NSLog(@"Didn't find database");
// get the source path to copy from
// NSString *dbSourcePath = [[NSBundle mainBundle] pathForResource:@"StayhealthyExercises-1" ofType:@"sqlite"];
NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
// copy db to documents
[[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil];
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "UPDATE stretchingexercises SET isFavorite = 'true' WHERE ID = '1'";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}else{
if (sqlite3_step(sqlStatement) == SQLITE_DONE){
NSLog(@"Success");
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
}
}

旧版本

- (NSString *)applicationDocumentsDirectory {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
return basePath;
}
- (IBAction)update:(id)sender {
@try {
NSString *docsPath = [self applicationDocumentsDirectory];
NSString *dbPath = [docsPath stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
if (!fileExists) {
NSLog(@"Found Database");
// get the source path to copy from
NSString *dbSourcePath = [[[NSBundle mainBundle] resourcePath  ]stringByAppendingPathComponent:@"StayhealthyExercises-1.sqlite"];
// copy db to documents
[[NSFileManager defaultManager] copyItemAtPath:dbSourcePath toPath:dbPath error:nil];
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
NSLog(@"An error has occured: %s", sqlite3_errmsg(db));
}
const char *sql = "UPDATE stretchingexercises SET isFavorite = 'true' WHERE ID = '1'";
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare(db, sql, -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}else{
if (sqlite3_step(sqlStatement) == SQLITE_DONE){
NSLog(@"Success");
}
}
sqlite3_finalize(sqlStatement);
}
@catch (NSException *exception) {
NSLog(@"Problem with prepare statement:  %s", sqlite3_errmsg(db));
}
@finally {
sqlite3_close(db);
}
}

用以下代码替换-(IBAction)update:(id)sender中第一个if块中获取dbPath的代码:

NSString *dbPath = [[NSBundle mainBundle] pathForResource:@"StayhealthyExercises-1" ofType:@"sqlite"];

尝试此代码检查文件是否存在

NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString* foofile = [documentsPath stringByAppendingPathComponent:@"CopyFileNameFromMainBundle.db"];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];

这应该返回NO

这只是错误日志的问题吗?在我看来应该是这样的:

....
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:dbPath];
if (!fileExists) {
NSLog(@"Didn't Find Database in Documents Directory!");
...
} else {
NSLog(@"Found Database in Documents Directory.");
}
...

相关内容

最新更新