In the development of iOS, often encounter database upgrade, modify some fields of delete table, this need us to deal with, the following analysis two kinds of database upgrade processing problem!
1. For example: We often use fmdb or other ways to create SQLite database, but because of the version iteration problem, often have the need to update the database, such as add fields, delete, modify fields and so on, how to upgrade the database and keep the original data valid, on the code:
Const static Nsinteger family_list_db_manager_ver = 1;
@implementation Ksfamilylistdbmanager
{
DBManager *_dbmgr;
}
+ (Instancetype) Sharedmanager
{
static Ksfamilylistdbmanager *s_instance = nil;
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
S_instance = [[Ksfamilylistdbmanager alloc] init];
});
return s_instance;
}
-(ID) init {
self = [super init];
if (self) {
_dbmgr = [DBManager defaultmgr];
[Self createtables];
Upgrade action
Nsinteger ver = [[Configdbmanager Sharedmanager] familylistdbmanagerversion];
if (ver < 1) {
[[Configdbmanager Sharedmanager] setfamilylistdbmanagerversion:family_list_db_manager_ver];
} else if (ver < family_list_db_manager_ver) {
[Self upgrade];
}
}
return self;
}
-(void) Upgrade {
Get the original version number that's stored well
Nsinteger oldversionnum = [[Configdbmanager Sharedmanager] familylistdbmanagerversion];
if (family_list_db_manager_ver <= oldversionnum | | oldversionnum = = 0) {
Return
}
Upgrade
[Self upgrade:oldversionnum];
Save the new version number to the library-here you can use Nsuserdefault storage
[[Configdbmanager Sharedmanager] setfamilylistdbmanagerversion:family_list_db_manager_ver];
}
-(void) Upgrade: (Nsinteger) oldversion {
if (oldversion >= family_list_db_manager_ver) {
Return
}
Switch (oldversion) {
Case 0:
Break
Case 1:
[Self Upgradefrom1to2];
Break
Case 2:
Break
Default
Break
}
Oldversion + +;
Recursion to determine if an upgrade is required
[Self upgrade:oldversion];
}
-(void) Upgradefrom1to2 {
Here execute the SQL statement to perform version 1 to version 2 of the update
}
2. If CoreData is used in iOS, the version iterations of the database upgrade are as follows:
If the iOS App is using CoreData and has database updates on the previous version (new tables, fields, and so on), the migration of the CoreData database will be done when the installer is overwritten, as follows:
1. Select your Mydata.xcdatamodeld file, select menu Editor->add Model Version, such as name: Mydata2.xcdatamodel
2. Set the current version
Select Superior Mydata.xcdatamodeld, select the current template as Mydata2 in the versioned Core Data model in Inspector
3. Modify the new data Model MYDATA2, add fields and tables to the new file
4. Delete the original class file and regenerate the following class.
In the Appdelegate
Nsdictionary *optionsdictionary = [nsdictionary dictionarywithobjectsandkeys:[nsnumber NumberWithBool:YES], Nsmigratepersistentstoresautomaticallyoption,[nsnumber Numberwithbool:yes], Nsinfermappingmodelautomaticallyoption,nil];
Nserror *error;
if (![ Self.persistentstorecoordinator
Addpersistentstorewithtype:nssqlitestoretype
Configuration:nil
Url:storeurl
Options:optionsdictionary
Error:&error]) {
NSLog (@ "Failed to add persistent store with type to persistent store Coordinator");
}
Add *optionsdictionary, original Options:nil changed to Options:optionsdictionary
5. Recompile the program.
How does an IOS database upgrade work?