提交 e6fc112b 编写于 作者: L leiiyb

add upgrade opt

Signed-off-by: Nleiiyb <leiyanbo@huawei.com>
上级 0b3e9ae2
......@@ -65,9 +65,9 @@
securityLevel: relationalStore.SecurityLevel.S1 // 数据库安全级别
};
// 假设当前数据库版本为3,表结构:EMPLOYEE (NAME, AGE, SALARY, CODES)
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // 建表Sql语句
// 假设当前数据库版本为3
relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => {
if (err) {
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
......@@ -75,7 +75,6 @@
}
console.info(`Succeeded in getting RdbStore.`);
// 这里执行升降级操作
// 当数据库创建时,数据库默认版本为0
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // 创建数据表
......@@ -83,17 +82,18 @@
store.version = 3;
}
// 如果数据库版本不为0且和当前数据库版本不匹配,需要进行升降级操作
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (store.version != 3 && store.version == 1) {
// version = 1:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES) => version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN ADDRESS TEXT", null);
// version = 1:表结构:EMPLOYEE (NAME, SALARY, CODES, ADDRESS) => version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (store.version != 3 && store.version == 2) {
// version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) => version = 3:表结构:EMPLOYEE (NAME, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN AGE INTEGER", null);
// version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) => version = 3:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES)
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
......@@ -118,10 +118,10 @@
name: 'RdbTest.db', // 数据库文件名
securityLevel: relationalStore.SecurityLevel.S1 // 数据库安全级别
};
// 假设当前数据库版本为3,表结构:EMPLOYEE (NAME, AGE, SALARY, CODES)
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // 建表Sql语句
// 假设当前数据库版本为3
relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => {
if (err) {
console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
......@@ -129,7 +129,6 @@
}
console.info(`Succeeded in getting RdbStore.`);
// 这里执行升降级操作
// 当数据库创建时,数据库默认版本为0
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // 创建数据表
......@@ -137,17 +136,18 @@
store.version = 3;
}
// 如果数据库版本不为0且和当前数据库版本不匹配,需要进行升降级操作
// 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本
if (store.version != 3 && store.version == 1) {
// version = 1:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES) => version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN ADDRESS TEXT", null);
// version = 1:表结构:EMPLOYEE (NAME, SALARY, CODES, ADDRESS) => version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本
if (store.version != 3 && store.version == 2) {
// version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) => version = 3:表结构:EMPLOYEE (NAME, SALARY, CODES, ADDRESS)
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN AGE INTEGER", null);
// version = 2:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) => version = 3:表结构:EMPLOYEE (NAME, AGE, SALARY, CODES)
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册