我維護一個SQLite數據庫在我的iPhone應用程序,所以,我想知道如何可以堅持一個單一的版本和更新。iPhone的SQLite數據庫路徑問題
其表示,如果要進行更新的分貝ü應該將它複製到文件夾
在我的iPhone模擬器一樣,每次文件夾改變我打開應用程序
如果其複製並修改被做了..如何應用程序反映在我的應用程序包 db的更新,所以當應用程序再次啓動時,應用程序將數據庫再次從我的包複製到文檔文件夾,以便能夠對其進行更改..
我的問題是,有時我會進行更改並再次加載應用程序以找到在變化消失了!
所有我做了什麼:
- 創建使用某種路徑上和資源的終端在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];
}
感謝您的幫助和時間 我想要做的是在用戶對文檔文件夾中的數據庫進行更改之後。 我希望將它複製到數據庫中的數據庫中。 – 2009-11-10 10:02:58
或自動發生 – 2009-11-10 10:03:59
Bundle始終爲只讀。沒有東西可以被複制回來。這就是爲什麼你必須將數據庫複製到Downloads文件夾,否則數據庫是隻讀的。 – mahboudz 2009-11-10 11:41:20