Ios SQLite数据库始终为空(FMDB)

Ios SQLite数据库始终为空(FMDB),ios,objective-c,sqlite,fmdb,Ios,Objective C,Sqlite,Fmdb,我正在做一个非常经典的例行程序,我已经做了多次,但在这个项目中,它不起作用。当我想使用我的数据库时,我会得到“没有这样的表”错误。这应该是错误的。我检查了bundle数据库和它的罚款;我检查了手机中的“结果”数据库,它完全是空的(没有结构,显然也没有数据) 这是我的数据库创建例程。我每次需要数据库时都会打电话给它 + (FMDatabase*)createAndCheckDataBase{ BOOL success; NSArray *docPaths = NSSearchPat

我正在做一个非常经典的例行程序,我已经做了多次,但在这个项目中,它不起作用。当我想使用我的数据库时,我会得到“没有这样的表”错误。这应该是错误的。我检查了bundle数据库和它的罚款;我检查了手机中的“结果”数据库,它完全是空的(没有结构,显然也没有数据)

这是我的数据库创建例程。我每次需要数据库时都会打电话给它

+ (FMDatabase*)createAndCheckDataBase{
    BOOL success;
    NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [docPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:@"database.sqlite"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    success = [fileManager fileExistsAtPath:databasePath];

    //The database already exist in the application folder, we don't need to create it
    if(success){
        return [FMDatabase databaseWithPath:databasePath];
    }

    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"database.sqlite"];
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];

    return [FMDatabase databaseWithPath:databasePath];
然后我只做
FMDatabase*db=[DatabaseManager createAndCheckDatabase]我应该得到我的数据库。但我没有

我使用Firefox中的sqlite管理器插件创建了sqlite数据库,在那里创建了结构,然后将其导入到包中

注意:成功总是返回true


欢迎任何帮助

如评论所述,您需要检查副本

下面是我的代码片段,它检查复制是否成功,并将错误详细信息返回给使用者

//
// notice our method to prepare the db returns bool and doesn't lose the error details
// the consumer can log, present, whatever - this is a library function
//
- (BOOL)ensureDatabasePrepared: (NSError **)error
{
    // <snip bunch of code>

    // copy db from template to library
    if (![[NSFileManager defaultManager] fileExistsAtPath:_dbPath])
    {
        NSLog(@"db not exists");

        // notice how we pass in the error ref passed to this function
        if (![[NSFileManager defaultManager] copyItemAtPath:dbTemplatePath toPath:_dbPath error:error])
        {
            return NO;
        }

        NSLog(@"copied");
    }
//
//请注意,我们准备db的方法返回bool,并且不会丢失错误详细信息
//消费者可以记录、显示,无论什么-这是一个库函数
//
-(BOOL)确保数据库准备就绪:(n错误**)错误
{
// 
//将数据库从模板复制到库
如果(![[NSFileManager defaultManager]fileExistsAtPath:\u dbPath])
{
NSLog(@“db不存在”);
//注意我们是如何将错误ref传递给这个函数的
如果(![[NSFileManager defaultManager]copyItemAtPath:dbTemplatePath-toPath:\u dbPath错误:错误])
{
返回否;
}
NSLog(@“复制”);
}

检查文件副本是否有效(提示:它返回一个
BOOL
值来告诉您),如果无效,请找出原因(提示:它返回一个
NSError
对象来告诉您).Yep,文件路径不正确,因此副本也不在正确的路径上。这是因为我几天前更改了捆绑包名称,.app没有更改。这导致路径中出现问题。使用正确的名称重命名所有内容可以修复此问题,并查看错误为我指明了方向;)谢谢!@Zil-我发现执行此操作非常有用复制后立即进行一个简单的“理智测试”——访问一些虚拟数据等@HotLicks是的,这是我应该有的习惯!感谢来到stack btw,我经常看到你有用的评论/答案:)