鴻蒙ArkTS Rdb學習
import Rdb from '@ohos.data.rdb';
import CommonConstants from '../common/constants/CommonConstants';
@Entry
@Component
struct Index {
?@State message: string = 'Hello World';
?private context = getContext(this);
?private rdbStore: Rdb.RdbStore;
?private tableName = 'student';
?private columns: Array<string> = ['id', 'name', 'age', 'gender']
?createRdbStore() {
? ?const STORE_CONFIG = { name: "RdbTest.db"}
? ?Rdb.getRdbStore(this.context, STORE_CONFIG, 1)
? ? ?.then((rdbStore) => {
? ? ? ?this.rdbStore = rdbStore;
? ? ? ?console.log("Get RdbStore successfully.")
? ? ?}).catch((err) => {
? ? ? ?console.log("Get RdbStore failed, err: " + `${err.code} + ${err.message}`)
? ? ?})
?}
?createTable() {
? ?let sqlCreate = 'CREATE TABLE IF NOT EXISTS student(id INTEGER PRIMARY KEY AUTOINCREMENT, name string, ' +
? ?'gender boolean, age INTEGER)';
? ?this.rdbStore.executeSql(sqlCreate)
? ? ?.then(() => {
? ? ? ?console.info('Create table done.')
? ? ?}).catch((error) => {
? ? ? ?console.log(`createTable failed ${error.code} ? ${error.message}`);
? ? ?})
?}
?insert() {
? ?const valueBucket = {
? ? ?"name": "Lisa",
? ? ?"age": 18,
? ? ?"gender": true
? ?}
? ?this.rdbStore.insert(this.tableName, valueBucket)
? ? ?.then((rowId) => {
? ? ? ?console.log("Insert is successful, rowId = " + JSON.stringify(rowId));
? ? ?}).catch((error) => {
? ? ? ?console.log(`insert failed ${error.code} ? ${error.message}`);
? ? ?})
?}
?query() {
? ?let predicates = new Rdb.RdbPredicates(this.tableName)
? ?this.rdbStore.query(predicates, this.columns)
? ? ?.then((resultSet) => {
? ? ? ?console.log("ResultSet column names: " + resultSet.columnNames)
? ? ? ?console.log("ResultSet column count: " + resultSet.columnCount)
? ? ? ?this.dealWithResultSet(resultSet)
? ? ?}).catch((error) => {
? ? ? ?console.log(`query failed ${error.code} ? ${error.message}`);
? ? ?})
?}
?dealWithResultSet(resultSet) {
? ?let count = resultSet.rowCount;
? ?if (count === 0 || typeof count === 'string') {
? ? ?console.log(`${CommonConstants.TABLE_TAG}` + 'Query no results!');
? ?} else {
? ? ?resultSet.goToFirstRow();
? ? ?const result = [];
? ? ?for (let i = 0; i < count; i++) {
? ? ? ?let tmp = {'id': 0, 'name': '', 'age': 0, 'gender': true}
? ? ? ?tmp.id = resultSet.getDouble(resultSet.getColumnIndex('id'));
? ? ? ?tmp.name = resultSet.getDouble(resultSet.getColumnIndex('name'));
? ? ? ?tmp.age = resultSet.getString(resultSet.getColumnIndex('age'));
? ? ? ?tmp.gender = resultSet.getDouble(resultSet.getColumnIndex('gender'));
? ? ? ?result[i] = tmp;
? ? ? ?resultSet.goToNextRow();
? ? ?}
? ? ?console.info(JSON.stringify(result))
? ?}
?}
?build() {
? ?Row() {
? ? ?Column() {
? ? ? ?Text(this.message)
? ? ? ? ?.fontSize(50)
? ? ? ? ?.fontWeight(FontWeight.Bold)
? ? ? ?Button('createRdbStore')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?this.createRdbStore();
? ? ? ? ?})
? ? ? ?Button('createTable')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?this.createTable();
? ? ? ? ?})
? ? ? ?Button('insert')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?this.insert();
? ? ? ? ?})
? ? ? ?Button('query')
? ? ? ? ?.onClick(() => {
? ? ? ? ? ?this.query();
? ? ? ? ?})
? ? ?}
? ? ?.width('100%')
? ?}
? ?.height('100%')
?}
}