Harmony(鸿蒙)开发手机数据库应用(Bee),超简单
创始人
2025-05-30 23:09:34

Harmony环境使用Bee入门向导

一、添加jar包

将bee相关的3个jar包复制到entry包下的libs目录,右击jar包,

选择:Add as Libray…  ,  在跳出的对话框中选择ok.

二、将相关配置注册到Bee

在启动的Ability ,添加相应的配置和注册信息。 若有自定义的配置在bee.properties则需要;则需要使 用:BeeConfigInit.init();

将上下文注册到Bee;将创建表和更新表的回调类,注册到Bee;

以后就可以直接使用Bee了。


public class UserDataAbility extends Ability {private static final String TAG = UserDataAbility.class.getSimpleName();private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);@Overridepublic void onStart(Intent intent) {super.onStart(intent);BeeConfigInit.init(); //若有自定义的配置在bee.properties则需要ContextRegistry.register(this.getApplicationContext()); //将上下文注册到BeeRdbOpenCallbackRegistry.register(new MyRdbOpenCallback()); //将创建表和更新表的回调类,注册到Bee
//      BeeRdbStoreRegistry.register(rdbStore);  //直接注册rdbStore对象也可以.  但需要自己去生成,配置信息也不好管理}
}

若有自定义的配置在bee.properties,将该文件放在entry\src\main\resources\rawfile目录下。

三、定义安装app时,创建表和更新表的类


package ohos.samples.dataability;import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.samples.dataability.bee.entity.*;
import ohos.samples.dataability.entity.Person;
import org.teasoft.honey.osql.autogen.Ddl;
import org.teasoft.honey.osql.core.HoneyContext;public class MyRdbOpenCallback extends RdbOpenCallback {private static final String TAG = "MyRdbOpenCallback";private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD000F00, TAG);@Overridepublic void onCreate(RdbStore store) {try{//        store.executeSql(   //手动写sql
//                "create table if not exists " +  "person (user_id integer primary key autoincrement, "
//                        + "name text not null, "  + "age integer)");HiLog.info(LABEL_LOG,"--------------------创建表.......开始.");String sql= Ddl.toCreateTableSQL(new Person()); //不想写sql可以自动生成HiLog.info(LABEL_LOG, "---------------create table sql:"+sql);store.executeSql(sql);//创建表样例:store.executeSql(Ddl.toCreateTableSQL(new LeafAlloc()));store.executeSql(Ddl.toCreateTableSQL(new Orders()));store.executeSql(Ddl.toCreateTableSQL(new Tb_inaccount()));store.executeSql(Ddl.toCreateTableSQL(new Tb_outaccount()));store.executeSql(Ddl.toCreateTableSQL(new TestUser()));} catch (Exception e) {HiLog.error(LABEL_LOG, "---------------create table:"+e.getMessage());}HiLog.info(LABEL_LOG, "------------onCreate  finished!");}@Overridepublic void onUpgrade(RdbStore store, int oldVersion, int newVersion) {HoneyContext.setCurrentAppDB(store);HiLog.info(LABEL_LOG,"--------------------更新表.......");HiLog.info(LABEL_LOG, "%{public}s", "DataBase upgrade");HoneyContext.removeCurrentAppDB();}}

四,可以在其它AbilitySlice中使用Bee操作数据库了

以下是select,update,insert,delete操作的例子。

主要语句如下:


Suid suid = BF.getSuid();  //简单的select,update,insert,delete操作
suid.insert(p);
suid.delete(new Person(), condition);
suid.update(p); //根据id修改对象
list = suid.select(new Person());

//BF是BeeFactoryHelper的简称,也可以如下用法:
//Suid suid=BeeFactoryHelper.getSuid();

详细代码如下:

    private void insert(Component component) {HiLog.info(LABEL_LOG, "----------------insert");try {Person p = new Person();p.setName(getRandomName());p.setAge(getRandomAge());suid.insert(p);HiLog.info(LABEL_LOG, "----------------insert结束.");} catch (Exception e) {HiLog.error(LABEL_LOG, "--------------insert--:" + e.getMessage());}query(true);}private void delete(Component component) {HiLog.info(LABEL_LOG, "----------------delete");try {Condition condition = BF.getCondition();condition.between("userId", 1, 2);suid.delete(new Person(), condition);} catch (Exception e) {HiLog.error(LABEL_LOG, "--------------insert--:" + e.getMessage());}query(true);}private void update(Component component) {HiLog.info(LABEL_LOG, "----------------update");try {Person p = new Person();p.setName("Tom_update");p.setAge(0);p.setUserId(1);suid.update(p); //根据id修改对象} catch (Exception exception) {HiLog.error(LABEL_LOG, "%{public}s", "update: dataRemote exception|illegalStateException");}query(true);}private void query(boolean queryAll) {HiLog.info(LABEL_LOG, "----------------query");getGlobalTaskDispatcher(TaskPriority.DEFAULT).asyncDispatch(() -> {List list = null;if (queryAll) {  //查所有list = suid.select(new Person());}else {list = suidRich.select(new Person(), 2, 5); //查从第2条开始的5条数据}appendText(list);});}

相关内容

热门资讯

电压放大器在钢筋剥离损伤识别试...   实验名称:钢筋剥离损伤识别试验  研究方向:无损检测  测试目的&#...
MOCO论文前几段精读 MoCo MoCo是CVPR 2020的最佳论文提名,算是视觉领域里,使...
【lua初级篇】基础知识和开发... 文章介绍 文章介绍 简述 工具安装配置和下载 快速看基础知识 一些常用的关键字一览 数据类型 tab...
Yuv422、Nv12转C#B... 1.1、Nv12转Bitmapint w = 1920;int h = 1080;i...
Linux互斥量和信号量的区别... 互斥量和信号量的区别 1.互斥量用于线程的互斥: 互斥:加锁解锁,是指某...
Git 和 GitHub 超入... 1.解决行结束符问题 需要在你的仓库中添加一个.gitattributes文件,标记正...
基于C++的AI五子棋游戏项目... 项目资源下载 基于C++的AI五子棋游戏项目源码压缩包下载地址基于C+...
#浅聊 webSocket (... 如果可以实现记得点赞分享,谢谢老铁~ 一,什么是webso...
Java SE API kno... Java SE API know how 字符串 紧凑字符串 java8 无论字符串的编码ÿ...
常用的VB函数 数学函数函数说明示例Sin(N)返回自变量N的正弦值Sin(0)=0 N为弧度Cos(N)返...
C++ 机房预约系统(五):管... 7.3 显示功能 功能描述: 显示学生信息或教师信息 功能实现: voi...
PIC单片机的一些问题 error 1347 can't find 0x16 words (0x16 withtotal) ...
完美日记母公司再度携手中国妇基... 撰稿 | 多客 来源 | 贝多财经 当春时节,梦想花开。和煦的三月暖阳,...
GDPU C语言 天码行空3 1. 分段函数 #includeint main(){double x,y;scanf("%lf",...
【瑞萨 MCU】开发环境搭建之... e2 studio e2 studio(简称为 e2 或 e2s)是瑞萨...
C语言内联汇编 之前我们介绍了一种C语言与汇编代码混合编程方式,就是两个文件分开编写,分...
Linux 网络编程学习笔记—... 一、TCP 服务的特点 传输层协议主要有 TCP 协议和 UDP 协议,前者相对于后者...
KubeSphere All ... KubeSphere All in one安装配置手册 1. 初始化 1.1 配置apt源 # vi...
学习软件测试怎么能缺少练手的软... 你好,我是凡哥。 最近收到许多自学自动化测试的小伙伴私信,学习了理论知识...
【面试题】浅谈css加载是否会... 大厂面试题分享 面试题库前后端面试题库 (面试必备) 推荐:...
直播带货系统开发的关键点、代码... 时下,直播的热度依然不减,而它的产物之一:直播带货系统&#...
一文读懂强化学习! 一.了解强化学习1.1基本概念强化学习是考虑智能体(Agent)与环境&...
Spring Cloud之一:... 目录 环境 Eureka工程的创建步骤 系列目录(持续更新。。。) S...
golang实现守护进程(2) 前言golang实现守护进程,包含功能:1. 守护进程只创建一次2. 平...
url 格式详解 统一资源定位系统(uniform resource locator; url ...
elasticsearch7.... elasticsearch版本:7.17.3 目标:实现对类型为text...
SpringBoot 加载系统... 开发环境: IDEA 2022.1.4+ MyBatis         代码参考:spri...
交换机概念和知识和命令 目录 一、华为交换机基础学习的一些重要概念和知识 二、交换机常用命令大全 三、不常用的交换机命令 ...
什么是 JavaScript ... 本文首发自「慕课网」,想了解更多IT干货内容,程序员圈内热闻࿰...
【C++】C++11——lam... 文章目录一、Lambda表达式引入二、Lambda表达式语法三、Lambda表达式交换两个值四、La...