鸿蒙应用开发—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()
})

参考

相关推荐
码力引擎21 小时前
【零基础学MySQL】第一章:MySQL介绍与安装
数据库·mysql·1024程序员节
小超嵌入式笔记21 小时前
【Keil5教程及技巧】耗时一周精心整理万字全网最全Keil5(MDK-ARM)功能详细介绍【建议收藏-细细品尝】
java·数据库·mongodb
hweiyu001 天前
Node.js+Koa2+MySQL 打造前后端分离项目(视频教程)
数据库·mysql·node.js
isNotNullX1 天前
一文讲清:数据清洗、数据中台、数据仓库、数据治理
大数据·网络·数据库·数据分析·1024程序员节
penguin_bark1 天前
C++调用MySQL数据库完整教程
数据库·c++·mysql
曾凡宇先生1 天前
无法远程连接 MySQL
android·开发语言·数据库·sql·tcp/ip·mysql·adb
苏小瀚1 天前
[MySQL] 事务和视图
数据库·mysql·1024程序员节
猫林老师1 天前
HarmonyOS分布式数据库深度应用
harmonyos
哈__1 天前
MongoDB 平替新方案:金仓多模数据库驱动电子证照国产化落地
数据库·1024程序员节
微学AI1 天前
国产数据库替代MongoDB的技术实践过程:金仓多模数据库在电子证照系统中的深度应用
数据库·人工智能·1024程序员节