【发布时间】:2012-07-06 18:35:16
【问题描述】:
我在我的 iPhone 应用程序中维护了一个 sqlite 数据库,所以我想知道你如何才能保留一个版本并更新一个版本。
它说如果要对数据库进行更新,您应该将其复制到文档文件夹
在我的 iPhone 模拟器上,每次打开应用程序时,文档文件夹都会发生变化
如果对其进行了复制和更改.. 应用程序如何将更新反映到我的应用程序包中的数据库 因此,当应用程序再次启动时,应用程序再次将数据库从我的包复制到文档文件夹,以便能够对其进行更改..
我的问题是有时我进行更改并再次加载应用程序发现更改已消失!
我所做的一切:
- 使用终端在某个路径上创建数据库,并在 xcode 中的资源文件夹上右键单击添加现有文件
- 我每次加载应用时都会这样做:
-(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];
}
【问题讨论】:
标签: iphone database sqlite path