当前位置:网站首页>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
边栏推荐
- 微信小程序
- 给定一个m*n的二维列表,查找一个数是否存在
- [yuanuniverse 3D competition]
- leetcode:19. 删除链表的倒数第 N 个结点
- Use lua+redis+openresty to realize concurrent optimization of e-commerce Homepage
- For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!
- 南京理工大学MOOC慕课:程序设计基础(Ⅰ)第8章测试选择题答案及解析
- Kotlin middle tail recursive function
- Character function and string function
- Interview experience - bytes
猜你喜欢

Qsort sort string

PS prompts "script error -50 general Photoshop error, how to solve it?

FD:文件描述符

For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!

Abstractqueuedsynchronizer (AQS) source code analysis - cyclicbarrier source code analysis

Storage of C language integer in memory

What should I do if a white page appears during MySQL installation

Ads Filter Design Wizard tool I

This article takes you to interpret the advertising advantages of tiktok

《网络是怎么样连接的》读书笔记 - FTTH
随机推荐
leetcode:打印两个有序链表的公共部分
Summary of problems and errors encountered in tidb4.0.0 (tiup deployment)
TiDB、mysql修改系统变量/常用语句(杀死process中的进程)
Abstractqueuedsynchronizer (AQS) source code analysis - cyclicbarrier source code analysis
Three ways to solve cross domain problems
[DB written interview 225] in Oracle, if the online redo log file is damaged, how to recover it?
STL教程3-类型转换static_cast、dynamic_cast、const_cast、reinterpret_cast方法
写文章的markdown规则
Using elastic stack to analyze Olympic data (II)
Doc common syntax, updating
Images, graphics and Applications (II)
4.4 Eval function replaces function
Summary of command execution knowledge points in CTF
Client construction and Optimization Practice
Thread pool source code analysis_ 01 futuretask source code analysis
For hand drawn graphics, covering multiple topics, CVPR 2022 sketchdl workshop begins to solicit contributions!
FD:文件描述符
WordPress obtains category name and link through category ID
Retrofit Extended reading
Figure out how MySQL works