2011-02-26 96 views
0

我在创建我的db文件时遇到问题。我使用这个功能:iPhone不会创建sqlite db文件

- (void)createEditableCopyOfDatabaseIfNeeded { 
    // Testing for existence 
    BOOL success; 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 
    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                 NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:DATABASE_NAME]; 

    NSLog(@"%@", writableDBPath); 

    success = [fileManager fileExistsAtPath:writableDBPath]; 
    if (success) { 
     NSLog(@"The file was here"); 
     return; 
    } 

    // The writable database does not exist, so copy the default to 
    // the appropriate location. 
    NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] 
           stringByAppendingPathComponent:DATABASE_NAME]; 
    success = [fileManager copyItemAtPath:defaultDBPath 
            toPath:writableDBPath 
            error:&error]; 
    NSLog(@"New file created"); 

    if(!success) 
    { 
     NSAssert1(0,@"Failed to create writable database file with Message : '%@'.", 
        [error localizedDescription]); 
    } 
} 

执行此总是生成日志“创建新的文件”,但如果我点我的取景器到数据库路径我找到一个空文件夹:没有创建文件!

你认为什么问题?

(我有使用相同功能的其他应用程序,效果很好!)

UPDATE:

如果我用“转到文件夹”取景器的功能,并在defaultDBPath粘贴,它告诉我我没有访问文件夹所需的权限

+0

什么是DATABASE_NAME的字符串值? – 2011-02-26 11:32:12

+0

#define DATABASE_NAME @“villaggio.db” – Abramodj 2011-02-26 11:36:39

回答

0

好吧,我解决了它......非常愚蠢的我!

只需将DATABASE_NAME设置为“villaggio。db”,而我的真实分贝名为“villaggio。sqlite”!

= D

1

无法立即发现您的通用代码存在问题,但请注意,NSLog(@"New file created");将始终发生,因为您未检查success。就像你的测试文件在那里,你需要检查success

if (success) 
    NSLog(@"New file created"); 

那么你至少会适当测试它是否认为一个文件被创建。如果没有,请仔细检查您的defaultDBPathwritableDBPath变量。