Harmony OSArkUIDemo关系数据库基本使用
Posted 华为开发者论坛
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Harmony OSArkUIDemo关系数据库基本使用相关的知识,希望对你有一定的参考价值。
在HarmonyOS 中关系数据库的增删改查是非常常见的一种技术,今天进行写一个demo对数据的增删改查进行详细的讲解,在开发之前我们需要学习“关系数据库”,“结果集”这两篇开发文档,接下来我们分为“Api使用”,“demo实现”,“运行效果”三个步骤进行描述
1. Api使用
1.1创建数据库
import dataRdb from @ohos.data.rdb
const STORE_CONFIG = name: "RdbTest.db"
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)"
let promise = dataRdb.getRdbStore(STORE_CONFIG, 1);
promise.then(async (rdbStore) =>
rdbStore.executeSql(SQL_CREATE_TABLE, null)
).catch((err) =>
expect(null).assertFail();
)
1.2 插入数据
const valueBucket =
"NAME": "Lisa",
"AGE": 18,
"SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret)
expect(1).assertEqual(ret)
console.log(TAG + "insert first done: " + ret))
1.3 删除数据
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
rdbStore.delete(predicates, function (err, rows)
console.log(TAG + "delete rows: " + rows))
1.4 修改数据
const valueBucket =
"NAME": "Rose",
"AGE": 22,
"SALARY": 200.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Lisa")
rdbStore.update(valueBucket, predicates, function (err, ret)
console.log(TAG + "updated row count: " + ret))
1.5 查询数据
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", "Rose")
rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet)
console.log(TAG + "resultSet column names:" + resultSet.columnNames)
console.log(TAG + "resultSet column count:" + resultSet.columnCount))
复制
1.6 结果集处理
resultSet.goToFirstRow()
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"))
复制
2. Demo实现
我们写五个text分别“创建数据库”,“插入数据”,“查询数据”,“修改数据”,“删除数据”,依次实现相应的点击事件,全部代码如下
import dataRdb from @ohos.data.rdb
struct MyRdb
rdb: dataRdb.RdbStore;
name: string= "Lisa"
private CreateRdbStore()
var that=this;
dataRdb.getRdbStore( name: "XiLeiRgb.db" , 1, function (err, rdbStore)
if (err)
console.log(获取失败 + err)
else
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)"
rdbStore.executeSql(SQL_CREATE_TABLE, null)
console.log(创建成功)
that.rdb=rdbStore;
)
private insertData()
const valueBucket =
"NAME": "Lisa",
"AGE": 18,
"SALARY": 100.5,
"CODES": new Uint8Array([1, 2, 3, 4, 5]),
let promise = this.rdb.insert("EMPLOYEE", valueBucket)
promise.then(async (ret) =>
console.log( "插入成功==>>insert first done: " + ret)
this.name="Lisa";
).catch((err) =>
)
private SelectData()
let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
predicates.equalTo("NAME", this.Harmony OS — ListContainer列表Horizon - a trustless harmony to ethereum bridge