2012-12-04 255 views
-2

我正在使用以下代码将数据库文件复制到其他文件夹中以创建临时写入文件。无法将数据库文件复制到其他文件夹

BOOL success; 
NSArray*dirPath; 
NSString*docDir; 
NSString*databasePath; 
NSString*[email protected]"EXPENSES"; 

//path for database 
dirPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
docDir=[dirPath objectAtIndex:0]; 
databasePath=[docDir stringByAppendingPathComponent:databaseName]; 

NSLog(@" docDir %@",docDir); 
//check if present 
NSFileManager*fm=[NSFileManager defaultManager]; 
success=[fm fileExistsAtPath:databasePath]; 

if(success) 
{ 
    NSLog(@"DATA BASE Already present"); 
} 
else 
{ 

    //Copy from bundle to DocumentsDirectory on first run. Where DB won't be available in DocumentsDirectory. 
    NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"EXPENSES" ofType:@""]; 
    NSError*error; 
    success=[fm copyItemAtPath:bundlePath toPath:databasePath error:&error]; 

    if(success) 
    { 
     NSLog(@"DATA BASE Created successfully"); 
    } 

} // End of else when DB not present in documents directory. 

但该文件没有复制,而不是应用程序崩溃与错误“的理由:‘* - [的NSFileManager copyItemAtPath:toPath:错误:]:源路径是零’”请帮我调试代码谢谢

回答

1

你在这里犯了两个错误。

  1. 你没有提到你的目标文件扩展名

    而不是NSString*[email protected]"EXPENSES";

    使用NSString*[email protected]"EXPENSES.sqlite";

  2. 此代码是实际的问题:

    NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"EXPENSES" ofType:@""]; 
    

这里你告诉NSFileManager找到名称为"EXPENSES"的文件,它的扩展名是""可能没有这些标准的文件。所以源路径是nil。这就是应用程序崩溃的原因。

通常数据库文件的扩展名为sqlite。替换你的代码,如:

NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"EXPENSES" ofType:@"sqlite"]; 
+0

我还没有提到任何扩展,在它工作之前,但我没有知道发生了什么突然它现在不工作 – Deepak

+0

@Deepak:请检查该文件仍然在主包 –

+0

感谢您的快速回复。它仍然在主要捆绑中。 – Deepak

0

尝试在ofType中添加ofType的数据库数据库扩展:
NSString*bundlePath=[[NSBundle mainBundle] pathForResource:@"EXPENSES" ofType:@"<extension>"];错误提示无法找到指定的文件。

+0

我还没有提到任何扩展,在它工作之前,但我不知道突然发生了什么,它不工作现在 – Deepak

+0

NSLog记录路径并检查文件是否存在以及文件名。如果它以前工作,没有理由为什么它应该突然停止工作。 – akdsouza

+0

感谢您的快速回复。现在我发现bundlePath是空的。这是什么问题(这是什么意思) – Deepak

0

我已经出来了这个问题。我不知道它背后的逻辑,我只是​​从SVN导出新的数据库,并添加到我的项目捆绑它的工作完美罚款

相关问题