当前位置:   article > 正文

【Harmony OS】【ArkUI】【Demo】关系数据库基本使用_harmonyos创建数据库

harmonyos创建数据库

 在HarmonyOS 中关系数据库的增删改查是非常常见的一种技术,今天进行写一个demo对数据的增删改查进行详细的讲解,在开发之前我们需要学习“关系数据库”,“结果集”这两篇开发文档,接下来我们分为“Api使用”,“demo实现”,“运行效果”三个步骤进行描述

1. Api使用

1.1创建数据库

image.png

  1. import dataRdb from '@ohos.data.rdb'
  2. const STORE_CONFIG = {name: "RdbTest.db" }
  3. 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)"
  4. let promise = dataRdb.getRdbStore(STORE_CONFIG, 1);
  5. promise.then(async (rdbStore) => {
  6. rdbStore.executeSql(SQL_CREATE_TABLE, null)
  7. }).catch((err) => {
  8. expect(null).assertFail();
  9. })

1.2 插入数据

image.png

  1. const valueBucket = {
  2. "NAME": "Lisa",
  3. "AGE": 18,
  4. "SALARY": 100.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. }
  7. rdbStore.insert("EMPLOYEE", valueBucket, function (err, ret) {
  8. expect(1).assertEqual(ret)
  9. console.log(TAG + "insert first done: " + ret)})

1.3 删除数据

image.png

  1. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  2. predicates.equalTo("NAME", "Lisa")
  3. rdbStore.delete(predicates, function (err, rows) {
  4. console.log(TAG + "delete rows: " + rows)})

1.4 修改数据

image.png

  1. const valueBucket = {
  2. "NAME": "Rose",
  3. "AGE": 22,
  4. "SALARY": 200.5,
  5. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  6. }
  7. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  8. predicates.equalTo("NAME", "Lisa")
  9. rdbStore.update(valueBucket, predicates, function (err, ret) {
  10. console.log(TAG + "updated row count: " + ret)})

1.5 查询数据

image.png

  1. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  2. predicates.equalTo("NAME", "Rose")
  3. rdbStore.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
  4. console.log(TAG + "resultSet column names:" + resultSet.columnNames)
  5. console.log(TAG + "resultSet column count:" + resultSet.columnCount)})

1.6 结果集处理

image.png

  1. resultSet.goToFirstRow()
  2. const id = resultSet.getLong(resultSet.getColumnIndex("ID"))
  3. const name = resultSet.getString(resultSet.getColumnIndex("NAME"))
  4. const age = resultSet.getLong(resultSet.getColumnIndex("AGE"))
  5. const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"))

2. Demo实现

我们写五个text分别“创建数据库”,“插入数据”,“查询数据”,“修改数据”,“删除数据”,依次实现相应的点击事件,全部代码如下

  1. import dataRdb from '@ohos.data.rdb'
  2. @Entry
  3. @Component
  4. struct MyRdb {
  5. rdb: dataRdb.RdbStore;
  6. @State name: string= "Lisa"
  7. private CreateRdbStore() {
  8. var that=this;
  9. dataRdb.getRdbStore({ name: "XiLeiRgb.db" }, 1, function (err, rdbStore) {
  10. if (err) {
  11. console.log('获取失败' + err)
  12. } else {
  13. 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)"
  14. rdbStore.executeSql(SQL_CREATE_TABLE, null)
  15. console.log('创建成功')
  16. that.rdb=rdbStore;
  17. }
  18. })
  19. }
  20. private insertData() {
  21. const valueBucket = {
  22. "NAME": "Lisa",
  23. "AGE": 18,
  24. "SALARY": 100.5,
  25. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  26. }
  27. let promise = this.rdb.insert("EMPLOYEE", valueBucket)
  28. promise.then(async (ret) => {
  29. console.log( "插入成功==>>insert first done: " + ret)
  30. this.name="Lisa";
  31. }).catch((err) => {
  32. })
  33. }
  34. private SelectData() {
  35. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  36. predicates.equalTo("NAME", this.name)
  37. console.log("查询条件====>"+this.name)
  38. this.rdb.query(predicates, ["ID", "NAME", "AGE", "SALARY", "CODES"], function (err, resultSet) {
  39. console.log("查询成功==>>resultSet column names:" + resultSet.columnNames)
  40. console.log("查询成功==>>resultSet column count:" + resultSet.columnCount)
  41. if(resultSet.rowCount>0){
  42. resultSet.goToFirstRow()
  43. const id = resultSet.getLong(resultSet.getColumnIndex("ID"))
  44. const name = resultSet.getString(resultSet.getColumnIndex("NAME"))
  45. const age = resultSet.getLong(resultSet.getColumnIndex("AGE"))
  46. const salary = resultSet.getDouble(resultSet.getColumnIndex("SALARY"))
  47. console.log( "id===>" + id+"===name==>"+name+"==age==>"+age+"==salary==>"+JSON.toString(salary))
  48. }else{
  49. console.log("暂无数据")
  50. }
  51. })
  52. }
  53. private updateData() {
  54. var that=this;
  55. let valueBucket = {
  56. "NAME": "Rose",
  57. "AGE": 22,
  58. "SALARY": 200.5,
  59. "CODES": new Uint8Array([1, 2, 3, 4, 5]),
  60. }
  61. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  62. predicates.equalTo("NAME", this.name)
  63. that.rdb.update(valueBucket, predicates, function (err, ret) {
  64. console.log("修改成功==>>updated row count: " + ret)
  65. that.name = "Rose";
  66. })
  67. }
  68. private deleteData() {
  69. let that=this
  70. let predicates = new dataRdb.RdbPredicates("EMPLOYEE")
  71. predicates.equalTo("NAME", this.name)
  72. that.rdb.delete(predicates, function (err, rows) {
  73. console.log("删除成功===>>delete rows: " + rows)
  74. })
  75. }
  76. build() {
  77. Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) {
  78. Text('创建数据库')
  79. .fontSize(50)
  80. .onClick(this.CreateRdbStore.bind(this))
  81. .fontWeight(FontWeight.Bold)
  82. Text('插入数据')
  83. .fontSize(50)
  84. .onClick(this.insertData.bind(this))
  85. .backgroundColor(Color.Red)
  86. .fontWeight(FontWeight.Bold)
  87. Text('查询数据')
  88. .fontSize(50)
  89. .onClick(this.SelectData.bind(this))
  90. .backgroundColor(Color.White)
  91. .fontWeight(FontWeight.Bold)
  92. Text('修改数据')
  93. .fontSize(50)
  94. .onClick(this.updateData.bind(this))
  95. .backgroundColor(Color.Red)
  96. .fontWeight(FontWeight.Bold)
  97. Text('删除数据')
  98. .fontSize(50)
  99. .onClick(this.deleteData.bind(this))
  100. .backgroundColor(Color.White)
  101. .fontWeight(FontWeight.Bold)
  102. }
  103. .width('100%')
  104. .height('100%')
  105. }
  106. }

界面效果如下所示

image.png

3. 运行效果

我们依次点击“创建数据库”,“插入数据”,“查询数据”,“修改数据”,“查询数据”,“删除数据”,“查询数据”的效果如下

image.png

 更多相关学习资料:
https://developer.huawei.com/consumer/cn/forum/topic/0202798055729860273?fid=0102683795438680754?ha_source=zzh

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/242330
推荐阅读
相关标签
  

闽ICP备14008679号