当前位置:网站首页>Android database upgrade

Android database upgrade

2022-06-21 08:36:00 WCanTouch

 

Database version upgrade

          Developing android When it comes to applications , Generally, when we are developing, we do not know what new functions will be introduced in the future , It is also possible to add business logic ( That is, update ), It is conceivable that our original database structure may not be applicable to the updated applications , Then the application may have problems when reading old data ; There are two ways to solve the above problems :
  1. Uninstall old version , Install new applications
            remarks : This is not good because the original data is lost ;

@Override
2     public void onUpgrade(SQLiteDatabase db, int oldVersion, int oldVersion) {
3       if(oldVersion!=oldVersion){
            dropAll(db);
            // Just recreate the new database 
              createTables(); The specific implementation is realized by yourself 
        }

7     }

   2. The software updates the database structure by itself ( Next, we will introduce the method of software self updating )
   

     First we create a new project and initialize the database ( At this time, the database version is 1.0)

 public class myDatabase extends SQLiteOpenHelper{
11     
12     public static final String CREATE_BOOK = "***.***.db";
       public static final String ALT_SMS_TABLE ="alter table sms rename to sms_tmp";
       public static final String DROP_SMS_TEMP ="DROP TABLE IF EXISTS sms_tmp";
       public static final String SMS_TEMP = "sms_tmp";
       public static final String SMS = "sms";
       public static final String
18     
19     private Context mContext;
20     
21 
22     public myDatabase(Context context) {
23         super(context, CREATE_BOOK, null, 1);  // The database version is 1   
24         mContext = context;
25         }
26     
27     
28     /* When the database is first created, call , Suitable for initializing , Create table   This method is not called when the database already exists */

29     @Override
30     public void onCreate(SQLiteDatabase db) {
31         db.execSQL(CREATE_BOOK);

35     }
36     
37     @Override
38     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
39        //
            db.beginTarnsaction();
            db.execSQL(ALT_SMS_TABLE);
            createSmsTables(db);
            // Copy the backup to the new table 
            copySmsTable(db);
            // Delete from backup table 
            db.execSQL(DROP_SMS_TEMP);
            db.endTransaction();

40     }
41     
42 }

 

Copy data to a new table :

private void copySmsTable(SQLiteDatebase db){

Cursor sms = db.rawQuery("PRAGMA table_info("+SMS_TMP+"),null");
if(sms!=null&&sms.getCount!=0){
   String[]columnNames = new String[sms.getCount()];
   int columnIndex = sms.getColumnIndex("name");
   if(columnIndex==-1){
       return;
   }
    int index = 0;
    for(sms.moveToFirst();!sms.isAfterLast();sms.moveToNext()){
       columnNames[index] = sms.getString(columnIndex);
       index++;
    }
    String columns = Arrays.toString(columnNames);
    columns = columns.substring(1,columns.length()-1);
    String sql = "INSERT INTO"+SMS+"("+columns+")"+"SELECT"+columns+"FROM"+SMS_TMP;
    db.execSQL(sql);
    sms.close();
    
    }

}

 

 

 

 

Degraded design key points

@Override
 2     public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
        // This method must be implemented    Otherwise, you can't downgrade later 
        // Even if this method is not implemented , If you need it later , Continue to realize                
                
        
     }

Reference link   Thank you very much :

         https://blog.csdn.net/leehong2005/article/details/9128501s

         https://www.cnblogs.com/wenjiang/p/4557196.html

 

原网站

版权声明
本文为[WCanTouch]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206210832425097.html