将SQLite数据库从bundle复制到文档进行写入?

时间:2014-01-18 04:46:43

标签: ios sql objective-c sqlite

所以我试图将当前在主包中的数据库复制到文档中,这样我就可以写入它而不是只能读取它。现在我有如下,它只是找不到数据库,NSLog(“找到数据库”);没有显示,因此复制不起作用。我已经在这方面工作了一段时间,但我无法弄明白。任何帮助都将不胜感激。

更新版本

   - (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);
}

}

OLD VERSION

- (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);
}

}

3 个答案:

答案 0 :(得分:1)

使用以下代码替换您的代码,以便在 - (IBAction)更新:(id)发件人的第一个if块中获取dbPath:

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

答案 1 :(得分:0)

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

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

这应该返回NO

答案 2 :(得分:0)

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

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