当前位置:网站首页>adroid SQLiteOpenHelper数据表结构升级
adroid SQLiteOpenHelper数据表结构升级
2022-04-22 09:42:00 【建人尹口】
最近在项目中遇到了一个新需求,需要增加用户在本地缓存中一个字段,由于用户缓存是根据所在群来进行分表保存的,在更新数据库升级时,发现要修改的表明是动态生成的,经过不断努力和网上的参考,终于找到了解决方法。
主要的逻辑还是放在自定义SQLiteOpenHelper类中的onUpgrade方法里,我们根据db参数可以获取所以的数据表,由于分表时表名都具有一定的规律,所以可以根据该规则来进行统一修改。下面就是笔者的实现逻辑,可供参考。
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//不同的数据库版本应该有不同的处理逻辑,目前只有本次升级
if (oldVersion == 1) {
//查询所有的数据表名
Cursor cursor = db.rawQuery("select name from sqlite_master where type='table' order by name", null);
while (cursor.moveToNext()) {
String name = cursor.getString(0);
if (!TextUtils.isEmpty(name) && name.startsWith("users_")) {
//根据匹配规则得到目标数据表,然后执行相应操作,本次只是新增一个字段而已
db.execSQL("ALTER TABLE " + name + " ADD COLUMN aliasName text");
}
}
//最后别忘了关闭cursor
cursor.close();
}
}
版权声明
本文为[建人尹口]所创,转载请带上原文链接,感谢
https://blog.csdn.net/u012758497/article/details/103091950
边栏推荐
- L3-005 dustbin distribution (30 points) (dijkstar)
- L2-030 冰岛人 (25 分) (最近公共祖先 思维
- P8资料大放送
- FUTR3D:一个统一的传感器融合3D检测框架
- stringstream的用法
- IOS开发之——数据库-基础知识介绍(01)
- Writing to remember the ~ goal and
- How to quickly turn off the classic FET - Kia MOS transistor
- 2022起重机司机(限桥式起重机)考试题库及在线模拟考试
- 一文学会text-justify,orientation,combine文本属性
猜你喜欢

P8 data broadcasting

Various location codes applied in transformer model

Heap overflow of kernel PWN basic tutorial

深度学习遥感场景分类数据集整理

项目实训-读报僵尸

Cloud native enthusiast weekly: looking for open source alternatives to netlify
![[age appropriate friends of interesting programming questions] (detailed explanation of the original title of leetcode)](/img/06/9f00f0a3d4105159d80b2b54fd9129.png)
[age appropriate friends of interesting programming questions] (detailed explanation of the original title of leetcode)

Kernel pwn 基础教程之 Heap Overflow

Text justify, orientation, combine text attributes

An analysis of the application of MOS tube series parallel drive - Kia MOS tube
随机推荐
浅析MOS管开关速度影响因素-KIA MOS管
杰理之AI Server【篇】
【Flutter 专题】125 图解自传 ACE_ICON.ttf 图标库
二极管工作原理
LC301. Remove invalid parentheses
[age appropriate friends of interesting programming questions] (detailed explanation of the original title of leetcode)
杰理之复位IO维持电平使用说明【篇】
2022-04-21 mysql-innodb存储引擎核心处理
L3-005 dustbin distribution (30 points) (dijkstar)
JS clich é about this, constructor, prototype
L2-033 simple calculator (25 points)
Project training - newspaper reading zombie
L2-030 冰岛人 (25 分) (最近公共祖先 思维
L3-005 垃圾箱分布 (30 分)(dijkstar
2022年危险化学品经营单位安全管理人员上岗证题库及在线模拟考试
解析功率mos管为何会被烧毁-KIA MOS管
SQL 数据类型
软件测试基础知识,看完就可以和面试官硬碰硬
Easy to use screenshot software
【SQL server】SQL 概览