在iPhone和模拟器上的SQLite数据库。

时间:2021-12-10 23:07:29

I create database and put *.sqlite file in recources folder in Xcode. Before opening database I copy it to documents folder from resources. This works fine on simulator, but does not work on iPhone. For some reasons database file does not exists in iPhone device. This is my code:

我创建数据库并放置*。在Xcode的资源文件夹中的sqlite文件。在打开数据库之前,我将它从参考资料中复制到documents文件夹。这在模拟器上工作得很好,但在iPhone上就不行。由于某些原因,iPhone设备中不存在数据库文件。这是我的代码:

   NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseFileName];


    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success = [fileManager fileExistsAtPath:databasePath];
    if(!success) // all the time it's YES, even if I delete database
    {
        // copy database from application folder
        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseFileName];
        BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];
        if (fileExists) { // It's NO, for some reasons database does not exist
            [fileManager removeItemAtPath:databasePath error:nil];
            [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
        }

        [fileManager release];
    }

    return databasePath;

For some reasons this line returns NO all the time

由于某些原因,这条线不会一直返回。

        BOOL fileExists = [fileManager fileExistsAtPath:databasePathFromApp];

the path in databasePathFromApp somthing like this

databasePathFromApp中的路径是这样的

/var/alexander/xxx-xxx-xxx-xxx..../AppName.app/database.sqlite

/var/alexander/xxx-xxx-xxx-xxx .... / AppName.app / database.sqlite

The question is: why database does not exists in recources?

问题是:为什么数据库在资源中不存在?

2 个解决方案

#1


3  

Finaly found the problem. iPhone device care about file name register, so "database.sqlite" and "DataBase.sqlite" is different files for device, but the same files for simulator. It's so weird.

终于找到这个问题。iPhone设备关心的是文件名注册,所以是“数据库”。sqlite”和“数据库。sqlite“是设备的不同文件,而模拟器的文件是相同的。它是如此奇怪。

I posed it in the blog Can't open file in iPhone device, but can in iPhone simulator

我把它放在了iPhone设备不能打开文件的博客里,但是可以在iPhone模拟器里打开

#2


0  

Delete database from resource & add it once again & check box appears at top with Copy items into destination group folder.

从资源中删除数据库并再次添加它&复选框出现在顶部,并将项目复制到目标组文件夹。

Clean targets & reset simulator.

清理目标和复位模拟器。

#1


3  

Finaly found the problem. iPhone device care about file name register, so "database.sqlite" and "DataBase.sqlite" is different files for device, but the same files for simulator. It's so weird.

终于找到这个问题。iPhone设备关心的是文件名注册,所以是“数据库”。sqlite”和“数据库。sqlite“是设备的不同文件,而模拟器的文件是相同的。它是如此奇怪。

I posed it in the blog Can't open file in iPhone device, but can in iPhone simulator

我把它放在了iPhone设备不能打开文件的博客里,但是可以在iPhone模拟器里打开

#2


0  

Delete database from resource & add it once again & check box appears at top with Copy items into destination group folder.

从资源中删除数据库并再次添加它&复选框出现在顶部,并将项目复制到目标组文件夹。

Clean targets & reset simulator.

清理目标和复位模拟器。