【问题标题】:Copying SQLite Database from bundle to documents for writing?将 SQLite 数据库从包复制到文档以进行编写?
【发布时间】:2014-01-18 04:46:42
【问题描述】:

所以我正在尝试将当前位于主包中的数据库复制到文档中,以便我可以写入它而不是仅仅能够从中读取。现在我有如下,它只是找不到数据库, 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);
}

}

【问题讨论】:

  • 那是因为你的路径不是指向 [NSBundle mainBundle] 而是指向 NSDocumentDirectory 之类的。
  • 你检查文件夹路径的文件了吗?
  • @retro 是的,它不存在
  • 你的代码没有进入 if 块?奇怪!
  • 您的代码中的 dbPath 指向什么,[NSBundle mainBundle] 或 NSDocumentDirectory?仔细查看 if (!fileExists) { . 之前的行

标签: ios sql objective-c sqlite


【解决方案1】:

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

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

【讨论】:

  • @retro 好的,所以现在我已经用我现在拥有的代码更新了我的答案,但是现在我得到了 NSLog("Success"),但是查询不起作用,我知道查询应该工作。我已尝试删除该应用程序,以便删除空白数据库(如果有的话),有什么建议吗?
  • @RobertSaunders - 所以当查询失败时,sqlite_errmsg 会报告什么?
  • @RobertSaunders 将数据库复制到文档目录后检查该表是否存在于数据库中。有时,您最终会在文档目录中创建一个数据库,而不是复制它。所以在文档目录中创建的新数据库不会有表。查询也可能失败,因为文档目录下的数据库中没有这样的表
【解决方案2】:

试试这个代码来检查文件是否存在

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

这应该返回NO

【讨论】:

  • 好的,所以现在我已经用我现在拥有的代码更新了我的答案,但是现在我得到了 NSLog("Success"),但是查询不起作用,我知道查询应该起作用。我已尝试删除该应用程序,以便删除空白数据库(如果有的话),有什么建议吗?
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-07
    • 2011-08-26
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多