提交 07afdeb3 编写于 作者: A Annie_wang

update docs

Signed-off-by: NAnnie_wang <annie.wangli@huawei.com>
上级 e7a61f77
...@@ -65,6 +65,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -65,6 +65,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
securityLevel: relationalStore.SecurityLevel.S1 // Database security level. securityLevel: relationalStore.SecurityLevel.S1 // Database security level.
}; };
// The current RDB store version is 3, and the table structure is 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 statement for creating a data table. 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 statement for creating a data table.
relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => { relationalStore.getRdbStore(this.context, STORE_CONFIG, (err, store) => {
...@@ -73,7 +74,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -73,7 +74,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
return; return;
} }
console.info(`Succeeded in getting RdbStore.`); console.info(`Succeeded in getting RdbStore.`);
// When the RDB store is created, the default version is 0.
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // Create a data table. store.executeSql(SQL_CREATE_TABLE); // Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version = 3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if (store.version != 3 && store.version == 1) {
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if (store.version != 3 && store.version == 2) {
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store. // Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
...@@ -97,6 +119,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -97,6 +119,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
securityLevel: relationalStore.SecurityLevel.S1 // Database security level. securityLevel: relationalStore.SecurityLevel.S1 // Database security level.
}; };
// The RDB store version is 3, and the table structure is 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 statement for creating a data table. 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 statement for creating a data table.
relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => { relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => {
...@@ -105,7 +128,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -105,7 +128,28 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
return; return;
} }
console.info(`Succeeded in getting RdbStore.`); console.info(`Succeeded in getting RdbStore.`);
// When the RDB store is created, the default version is 0.
if (store.version == 0) {
store.executeSql(SQL_CREATE_TABLE); // Create a data table. store.executeSql(SQL_CREATE_TABLE); // Create a data table.
// Set the RDB store version, which must be an integer greater than 0.
store.version = 3;
}
// If the RDB store version is not 0 and does not match the current version, upgrade or downgrade the RDB store.
// For example, upgrade the RDB store from version 1 to version 2.
if (store.version != 3 && store.version == 1) {
// Upgrade the RDB store from version 1 to version 2, and change the table structure from EMPLOYEE (NAME, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS).
store.executeSql("ALTER TABLE EMPLOYEE ADD COLUMN AGE INTEGER", null);
store.version = 2;
}
// For example, upgrade the RDB store from version 2 to version 3.
if (store.version != 3 && store.version == 2) {
// Upgrade the RDB store from version 2 to version 3, and change the table structure from EMPLOYEE (NAME, AGE, SALARY, CODES, ADDRESS) to EMPLOYEE (NAME, AGE, SALARY, CODES).
store.executeSql("ALTER TABLE EMPLOYEE DROP COLUMN ADDRESS TEXT", null);
store.version = 3;
}
// Perform operations such as adding, deleting, modifying, and querying data in the RDB store. // Perform operations such as adding, deleting, modifying, and querying data in the RDB store.
...@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -185,13 +229,22 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
```js ```js
let predicates = new relationalStore.RdbPredicates('EMPLOYEE'); let predicates = new relationalStore.RdbPredicates('EMPLOYEE');
predicates.equalTo('NAME', 'Rose'); predicates.equalTo('NAME', 'Rose');
store.query(predicates, ['ID', 'NAME', 'AGE', 'SALARY', 'CODES'], (err, resultSet) => { store.query(predicates, ['ID', 'NAME', 'AGE', 'SALARY'], (err, resultSet) => {
if (err) { if (err) {
console.error(`Failed to query data. Code:${err.code}, message:${err.message}`); console.error(`Failed to query data. Code:${err.code}, message:${err.message}`);
return; return;
} }
console.info(`ResultSet column names: ${resultSet.columnNames}`); console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`);
console.info(`ResultSet column count: ${resultSet.columnCount}`); // resultSet is a cursor of a data set. By default, the cursor points to the -1st record. Valid data starts from 0.
while (resultSet.goToNextRow()) {
const id = resultSet.getLong(resultSet.getColumnIndex("ID"));
const name = resultSet.getString(resultSet.getColumnIndex("NAME"));
const age = resultSet.getLong(resultSet.getColumnIndex("AGE"));
const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"));
console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
}
// Release the data set memory.
resultSet.close();
}) })
``` ```
...@@ -221,7 +274,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP ...@@ -221,7 +274,7 @@ The following table lists the APIs used for RDB data persistence. Most of the AP
console.info('Succeeded in deleting RdbStore.'); console.info('Succeeded in deleting RdbStore.');
}); });
} }
} }
``` ```
FA model: FA model:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册