鸿蒙应用开发—ZDbUtil高效使用数据库

文章目录

介绍

ZDbUtil是一款基于SQLite的鸿蒙数据库框架,通过注解标注实体类与属性,让数据更能抽象化简化原生RdbStore的使用。同时支持V1和V2状态管理管理。

SQLite在鸿蒙上的基本使用可以参考:鸿蒙应用开发---数据持久化之SQLite

下载安装

在每个har/hsp模块中,通过ohpm工具下载安装库:

复制代码
ohpm install @hzw/zdb

基本使用

注解

Table

表名为类名。

Id
  • 参数
    • primaryKey: 是否主键
    • autoIncrement: 是否自增
Column
  • 参数 :
    • type: 列类型,查看ColumnType
    • notNull: 是否为空
    • unique: 是否唯一
OneToOne
  • 参数 :
    • joinTable: 关联表的类
    • relatedIdProperty: 关联id属性名

使用方法

定义实体类

只要添加了@Table注解,@Id注解,@Column注解

在初始化数据库时,会自动创建表,并添加列。

typescript 复制代码
import { Column, ColumnType, Id, OneToOne, Table } from '@hzw/zdb';

// 分类
@Table()
export class StudentClassify {
  // id
  @Id({ primaryKey: true, autoIncrement: true })
  @Column({ type: ColumnType.Integer })
  id?: number
  // 名称
  @Column({ type: ColumnType.Text })
  name?: string
  // 创建时间
  @Column({ type: ColumnType.Integer })
  createTime?: number
  // 更新时间
  @Column({ type: ColumnType.Integer })
  editTime?: number | undefined
}

// 学生
@Table()
export class StudentInfo {
  // id
  @Id({ primaryKey: true, autoIncrement: true })
  @Column({ type: ColumnType.Integer })
  id?: number | undefined
  // 标题
  @Column({ type: ColumnType.Text })
  title?: string | undefined
  // 创建时间
  @Column({ type: ColumnType.Integer })
  createTime?: number | undefined
  // 是否管理员
  @Column({ type: ColumnType.Boolean })
  isManager?: boolean | undefined
  // 分类id
  @Column({ type: ColumnType.Integer })
  classifyId?: number | undefined
  // 分类
  @OneToOne({ joinTable: StudentClassify, relatedIdProperty: "classifyId" })
  classify?: StudentClassify | undefined
}
初始化数据库并根据被@Table注解的类创建表
typescript 复制代码
ZDbUtil.initDatabase({
  context: this.context
})
创建表

如果类不被@Table注解,则需要手动

typescript 复制代码
// 定义表的数据结构
const student: StudentInfo2 = {
  "id": 0,
  "title": "",
  "createTime": 0,
  "isManager": false,
  "classifyId": 0,
}
// 创建表
// 第一个参数是表的数据结构
// 第二个参数是表名
// 第三个参数是id名
ZDbUtil.initTableByName(student, "Student", "id").then(() => {
  promptAction.showToast({ message: "创建成功" });
})
查数据

如果查询的数据类型有被@Table注解,则通过这种方式查询数据

typescript 复制代码
ZDbUtil.querySqlBuilder<StudentClassify>(StudentClassify)
  .limitAs(this.pageSize)
  .offsetAs(this.page * this.pageSize)
    // ...
  .query()
  .then((data) => {
  })

否则通过这种方式查询数据

typescript 复制代码
// 定义表的数据结构
const student: StudentInfo2 = {
  "id": 0,
  "title": "",
  "createTime": 0,
  "isManager": false,
  "classifyId": 0,
}
// 定义关联表的数据结构
const classify: StudentClassify2 = {
  "id": 0,
  "name": "",
  "createTime": 0,
  "editTime": 0,
}
// 查询表
// 第一个参数是表的数据结构
// 第二个参数是表名
// 第三个参数是id名
ZDbUtil.querySqlObjBuilder(student, "Student", "id")
  // 关联表
  // 第一个参数是关联表的数据结构
  // 第二个参数是关联表的表名
  // 第三个参数是关联表的id名
  // 第四个参数是与关联表关联的id字段名
  // 第五个参数是存放关联表数据的字段名
  .initJoinTableInfo(classify, "Classify", "id", "classifyId", "classify")
  .limitAs(this.pageSize)
  .offsetAs(this.page * this.pageSize)
  .query()
  .then((data) => {
    this.list = data;
  })
插入数据

data类型确定,且被@Table注解,直接插入数据

typescript 复制代码
ZDbUtil.insertOrReplace(data).then(() => {
  promptAction.showToast({ message: "添加成功" });
})

data类型是Object,通过传入被@Table注解的类,插入数据

typescript 复制代码
// 插入数据
// 第一个参数为插入的数据
// 第二个参数为被@Table注解的类,数据将会被插入到与注解类关联的表中
ZDbUtil.insertOrReplaceByCls(data, StudentClassify).then(() => {
  promptAction.showToast({ message: "添加成功" });
})

data类型是Object,通过传入表名插入数据

typescript 复制代码
// 插入数据到数据库
// 第一个参数是插入的数据
// 第二个参数是表名
ZDbUtil.insertOrReplaceByName(data, "Student").then(() => {
  promptAction.showToast({ message: "添加成功" });
  this.loadData()
})
删除数据

data类型确定,且被@Table注解,直接删除数据

typescript 复制代码
ZDbUtil.delete(item).then(() => {
  promptAction.showToast({ message: "删除成功" });
})

data类型是Object,通过传入被@Table注解的类,删除数据

typescript 复制代码
ZDbUtil.deleteByCls(data, StudentInfo).then(() => {
  promptAction.showToast({ message: "删除成功" });
})

直接通过传入表名,id名,id的值删除数据

typescript 复制代码
// 第一个参数是表名
// 第二个参数是id名
// 第三个参数是id的值
ZDbUtil.deleteByName("Student", "id", item.id).then(() => {
  promptAction.showToast({ message: "删除成功" });
  this.loadData()
})
清空数据

根据被@Table注解的类,清空与之关联的表的所有数据

typescript 复制代码
ZDbUtil.clear(StudentInfo).then(() => {
  promptAction.showToast({ message: "清空成功" });
})

根据表名,清空该表的所有数据

typescript 复制代码
ZDbUtil.clearByName("Classify").then(() => {
  promptAction.showToast({ message: "清空成功" });
  this.selectClassify = undefined
  this.loadClassify()
  this.loadData()
})

参考

相关推荐
A__tao14 分钟前
在线 SQL 转 Flask-SQLAlchemy
数据库·sql·flask
04Koi.18 分钟前
Redis--渐进式遍历
数据库·redis·缓存
铁打的阿秀1 小时前
navicat 创建Oracle连接报错:ora28040 没有匹配的验证协议
数据库·oracle
小杨xyyyyyyy1 小时前
Mysql - 日志相关问题
数据库·mysql·面试
交响梦2 小时前
医院信息系统平台总体架构原则
大数据·数据库·人工智能·架构·健康医疗
程序员沉梦听雨2 小时前
【实战篇】exists语法解析
数据库·mysql
码上飞扬2 小时前
SQL Server运维实战:十大高频问题分析与解决方案
运维·数据库·oracle
别说我什么都不会3 小时前
OpenHarmony源码分析之分布式软总线:trans_service模块(5)/TCP会话管理
分布式·嵌入式·harmonyos
小画家~3 小时前
第二:go 链接mysql 数据库
数据库·mysql
先睡3 小时前
Spring Boot 的自动装配
数据库