iOS Database update upgrade scenario

Source: Internet
Author: User

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 the field, etc., how to upgrade the database and keep the original data valid, 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 Operation          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 stored well     NSInteger  Oldversionnum = [[configdbmanager sharedmanager] familylistdbmanagerversion];    if  (family_list_db_manager_ver <= oldversionnum | |  oldversionnum == 0)  {        return;     }        //Upgrade     [self upgrade:o ldversionnum];        //  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 {    //Execute SQL statement here   perform version 1 to version 2 of the update}


iOS Database update upgrade scenario

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.