2009-11-10 127 views
1

我维护一个SQLite数据库在我的iPhone应用程序,所以,我想知道如何可以坚持一个单一的版本和更新。iPhone的SQLite数据库路径问题

其表示,如果要进行更新的分贝ü应该将它复制到文件夹

在我的iPhone模拟器一样,每次文件夹改变我打开应用程序

如果其复制并修改被做了..如何应用程序反映在我的应用程序包 db的更新,所以当应用程序再次启动时,应用程序将数据库再次从我的包复制到文档文件夹,以便能够对其进行更改..

我的问题是,有时我会进行更改并再次加载应用程序以找到在变化消失了!

所有我做了什么:

  1. 创建使用某种路径上和资源的终端在Xcode文件夹右键单击添加现有文件
  2. 我这个每次做我加载应用程序的数据库:
-(void)DBinit 
{ 
    // Setup some globals 
    databaseName = @"articlesdb.sql"; 

    // Get the path to the documents directory and append the databaseName 
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDir = [documentPaths objectAtIndex:0]; 
    databasePath = [[documentsDir stringByAppendingPathComponent:databaseName] copy]; 
    NSLog(databasePath); 
    // Execute the "checkAndCreateDatabase" function 
    [self checkAndCreateDatabase]; 
} 

-(void)checkAndCreateDatabase 
{ 
    // Check if the SQL database has already been saved to the users phone, if not then copy it over 
    BOOL success; 

    // Create a FileManager object, we will use this to check the status 
    // of the database and to copy it over if required 
    NSFileManager *fileManager = [NSFileManager defaultManager]; 

    // Check if the database has already been created in the users filesystem 
    success = [fileManager fileExistsAtPath:databasePath]; 

    // If the database already exists then return without doing anything 
    if(success) 
    { 
     [fileManager removeItemAtPath:databasePath error:nil]; 
     return; 
    } 

    // If not then proceed to copy the database from the application to the users filesystem 

    // Get the path to the database in the application package 
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; 

    // Copy the database from the package to the users filesystem 
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil]; 

    fileManager release]; 
} 

回答

1

Xcode中,与iPhone模拟器相结合,为您的应用,将间每个版本创建一个新的app文件夹将旧文档文件夹及其内容覆盖到新的应用程序文件夹中。您需要检查文档文件夹的内容,并确定您的Bundle中的文件是否适合用于文档文件夹中的文件。你应该明智而不是轻率地做出这个决定。

这是正确的行为。您的软件的用户可能对数据库做了一些重大更改和修改。他们可能不喜欢它,如果升级一个小错误修复程序或甚至功能增强功能时,它们的数据库更改,高分,修改后的图像或应用程序保存的任何内容突然被替换为通用版本。

你应该做的是比较新数据库中的数据与用户所拥有的数据,而对于SQLite,应该将用户的条目导入或复制到新的数据库中,然后可以移动将新数据库移至文档文件夹,删除用户的数据库,但将其替换为具有用户数据和新数据的数据库。

简而言之:确保您不会用您的套件中的新数据库替换现有的数据库,因为这会吹掉用户的更改。

+0

感谢您的帮助和时间 我想要做的是在用户对文档文件夹中的数据库进行更改之后。 我希望将它复制到数据库中的数据库中。 – 2009-11-10 10:02:58

+0

或自动发生 – 2009-11-10 10:03:59

+0

Bundle始终为只读。没有东西可以被复制回来。这就是为什么你必须将数据库复制到Downloads文件夹,否则数据库是只读的。 – mahboudz 2009-11-10 11:41:20