From e6fc112be8f0be2c2c0aeb1f68468d446961def7 Mon Sep 17 00:00:00 2001 From: leiiyb Date: Thu, 13 Apr 2023 14:02:15 +0800 Subject: [PATCH] add upgrade opt Signed-off-by: leiiyb --- .../database/data-persistence-by-rdb-store.md | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/zh-cn/application-dev/database/data-persistence-by-rdb-store.md b/zh-cn/application-dev/database/data-persistence-by-rdb-store.md index bbf96ee76b..558ae78b26 100644 --- a/zh-cn/application-dev/database/data-persistence-by-rdb-store.md +++ b/zh-cn/application-dev/database/data-persistence-by-rdb-store.md @@ -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; } -- GitLab