我在應用商店有1.0版本的應用程序,它使用sqlite數據庫讀取數據。
現在我想用數據庫文件中的更新將我的版本更新到1.1。
在設備上安裝應用程序時使用開發人員證書時,由於數據庫文件已存在於文檔文件夾中,因此未更新數據庫,因此我必須手動刪除應用程序並重新安裝該應用程序。
我的問題是,當任何用戶更新應用程序時,數據庫是否也會根據當前版本進行更新。
歡迎任何建議。
感謝當iPhone上的應用程序版本更改時,sqlite數據庫更新
17
A
回答
14
我相信有很多方法可以做到這一點(和很多方面優於礦一樣),但我處理這些問題的方式如下:
首先,我在定義一個常量應用程序(一個將首先加載)的第一個.h文件中,以指示首次加載,並將其設置爲0:
#define FirstTime 0
現在你要知道,我有意向保存此常數的值在Documents文件夾中以備將來參考,因此我使用共享數據實例。在viewDidLoad
我做如下測試:
//if first time run of this version
if([MyDataModel sharedInstance].count < (FirstTime + 1))
{
//do what you need to do as the first time load for this version
[MyDataModel sharedInstance].count++
//save the count value to disk so on next run you are not first time
//this means count = 1
}
現在關鍵是你的新的應用程序版本(比如1.1)。我改變FirstTime
以2:
#define FirstTime 2
由於光盤上保存的第一時間值是1,這意味着你會被if語句上面被抓住,因此在它裏面,你可以做任何你想要的喜歡刪除舊錶並用新編組重新創建它們。
再次不是那麼輝煌,而是解決了案子!
6
您可以使用.plist
還有:
- (void)isItTheFirstTimeAfterUpdate {
NSString *versionnum;
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourplist.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:path]) {
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"yourplist" ofType:@"plist"];
[fileManager copyItemAtPath:bundle toPath:path error:&error];
}
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
versionnum = @"";
//it can be installed by user (for ex. it is 1.3 but first installed), no plist value is set before
if(([savedStock objectForKey:@"versionnum"]) && (![[savedStock objectForKey:@"versionnum"] isEqualToString:@""])){
versionnum = [savedStock objectForKey:@"versionnum"];
}
//to get the version of installed/updated-current app
NSString *myversion = [NSString stringWithFormat:@"%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]];
//if no version has been set-first install- or my version is the latest version no need to do sth.
if ([versionnum isEqualToString:myversion] || [versionnum isEqualToString:@""]) {
NSLog(@"Nothing has to be done");
}
else {
[self cleanDB];//i have clean tables and create my new db tables maybe logout the user etc.
[savedStock setObject:[NSString stringWithString:myversion] forKey:@"versionnum"];//setting the new version
[savedStock writeToFile:path atomically:YES];
}
}
而且你可以調用函數在應用程序啓動或在您的主視圖控制器的視圖控制器..你的選擇。
希望它有幫助。
12
該方法依賴於NSUserDefaults
。這個想法是從NSUserDefaults
獲得以前的應用程序版本號(如果存在),並將其與當前版本進行比較。
如果以前的應用程序版本是<
而不是當前版本,或者以前的版本是nil
,代碼將執行數據庫升級。這意味着即使應用已經發布在AppStore上,也可以使用這種方法。它會在應用程序更新期間將數據庫升級到新版本。
這是一個plist文件:
有其由版本號和相應的升級版本的一組SQL查詢的陣列。 假設以前的版本是1.2並且實際版本是1.4,代碼僅從版本1.2執行升級到1.4。如果以前的版本是1.3並且當前版本是1.4,那麼代碼只能從1.3升級到1.4。 如果以前的版本是零代碼執行升級到1.1然後到1.2然後到1.3,最後到1.4。
NSString * const VERSION_KEY = @"version";
-(void)upgradeDatabaseIfRequired{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *previousVersion=[defaults objectForKey:VERSION_KEY];
NSString *currentVersion=[self versionNumberString];
if (previousVersion==nil || [previousVersion compare: currentVersion options: NSNumericSearch] == NSOrderedAscending) {
// previous < current
//read upgrade sqls from file
NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"UpgradeDatabase" ofType:@"plist"];
NSArray *plist = [NSArray arrayWithContentsOfFile:plistPath];
if (previousVersion==nil) {//perform all upgrades
for (NSDictionary *dictionary in plist) {
NSString *version=[dictionary objectForKey:@"version"];
NSLog(@"Upgrading to v. %@", version);
NSArray *sqlQueries=[dictionary objectForKey:@"sql"];
while (![DB executeMultipleSql:sqlQueries]) {
NSLog(@"Failed to upgrade database to v. %@, Retrying...", version);
};
}
}else{
for (NSDictionary *dictionary in plist) {
NSString *version=[dictionary objectForKey:@"version"];
if ([previousVersion compare: version options: NSNumericSearch] == NSOrderedAscending) {
//previous < version
NSLog(@"Upgrading to v. %@", version);
NSArray *sqlQueries=[dictionary objectForKey:@"sql"];
while (![DB executeMultipleSql:sqlQueries]) {
NSLog(@"Failed to upgrade database to v. %@, Retrying...", version);
};
}
}
}
[defaults setObject:currentVersion forKey:VERSION_KEY];
[defaults synchronize];
}
}
- (NSString *)versionNumberString {
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
NSString *majorVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
return majorVersion;
}
相關問題
- 1. 更新應用程序時更改SQLite數據庫結構
- 2. 在iPhone上更新Sqlite數據庫應用程序
- 3. 如何在應用程序版本更新時在iPhone上的SQLite數據庫中維護數據?
- 4. Android SQLite數據庫更新表時更新應用程序
- 5. 當更新應用程序時移動ios sqlite數據庫
- 6. Android更改數據庫的新版本關閉應用程序
- 7. 更新iPhone SQLite數據庫
- 8. SQLite數據庫更新在iPhone應用程序
- 9. 將版本更新到Appstore時如何更新sqlite數據庫。
- 10. 更改SQLite數據庫版本
- 11. 更改SQLite數據庫版本號
- 12. iOS中的Sqlite數據庫修改和應用程序更新
- 13. 更新sqlite數據庫版本?
- 14. 更新iPhone應用程序刪除舊版本的數據
- 15. 當服務器數據庫更新時更新sqlite數據庫
- 16. Sqlite數據庫不更新與應用程序更新
- 17. 更新SQLite數據庫時,應用程序與數據庫一起發佈的新版本
- 18. 更新用於iphone的sqlite數據庫
- 19. 如何更新我的iPhone應用程序上的數據庫?
- 20. 更新本地sqlite數據庫windows手機8應用程序
- 21. 當底層SQLite數據庫發生更改時更新UITableView
- 22. 數據庫沒有更新的應用程序版本2
- 23. iPhone應用程序更新(在sqlite的使用核心數據)
- 24. 更新應用程序時替換SQLite數據庫內容
- 25. 通過iTunes/AppStore更新應用程序時SQLite數據庫更新
- 26. 更新html表格版本(網絡應用程序)數據庫
- 27. Phonegap應用程序數據庫版本更新後保留
- 28. SQLiteOpenHelper即使在數據庫版本更改並卸載後,也不會更新數據庫Android應用程序
- 29. 當更新iPhone應用程序到新的通用版本時丟失了數據
- 30. iPhone應用程序數據庫:MySQL&SQLite
數據是隻讀還是讀/寫? – 2012-01-09 20:04:40
數據是隻讀的。 – 2012-01-09 20:06:03