Harmony(鸿蒙)开发手机数据库应用(Bee),超简单

news/2024/7/21 9:55:48 标签: harmonyos, 数据库, Bee

Bee_1">Harmony环境使用Bee入门向导

一、添加jar包

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

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

Bee_11">二、将相关配置注册到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);
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        BeeConfigInit.init(); //若有自定义的配置在bee.properties则需要
        ContextRegistry.register(this.getApplicationContext()); //将上下文注册到Bee
        RdbOpenCallbackRegistry.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);
    @Override
    public 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!");
    }

    @Override
    public 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();
    }

}

Bee_94">四,可以在其它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<Person> list = null;
            if (queryAll) {  //查所有
                list = suid.select(new Person());
            }else {
                list = suidRich.select(new Person(), 2, 5); //查从第2条开始的5条数据
            }
            appendText(list);
        });
    }

http://www.niftyadmin.cn/n/164613.html

相关文章

献给春天的演讲:扎根

【#献给春天的演讲#&#xff1a;扎根】终究有一天&#xff0c;我们会变成一棵参天大树。 【原文】 我是张颂文&#xff0c;我是一名演员&#xff0c;今年春节&#xff0c;我在社交媒体上面收到最多的一个评论&#xff1a;恭喜你&#xff0c;张颂文&#xff01;你的春天来了。…

结构体内存对齐,位段,枚举相关问题.

本文标签: 结构体内存对齐 结构位段问题 枚举 文章目录 目录 文章目录 一、结构体的内存对齐. 1.计算结构体大小 2.为什么存在内存对齐? 二、结构体的位段问题. 三、枚举 1.枚举类型定义 2.枚举的优点 为什么使用枚举? 总结 一、结构体的内存对齐. 1.计算结构体大小 知道…

day01_Java概述丶环境搭建

Java背景知识 Java概述 概述&#xff1a;计算机语言就是人与计算机之间进行信息交流沟通的一种特殊语言。所谓计算机编程语言&#xff0c;就是人们可以使用编程语言对计算机下达命令&#xff0c;让计算机完成人们需要的功能。 Java语言&#xff1a;是美国Sun公司&#xff08…

国务院关于职工工作时间的规定

&#xff08;1994年2月3日中华人民共和国国务院令第146号发布 根据1995年3月25日《国务院关于修改<国务院关于职工工作时间的规定>的决定》修订&#xff09; 第一条 为了合理安排职工的工作时间和休息时间&#xff0c;维护职工的休息权利&#xff0c;调动职工的积极性…

CleanMyMac4.3.0最新版本具体功能详情介绍

CleanMyMac自身拥有一个安全数据可&#xff0c;它是一个项目列表&#xff0c;拥有一定的规格&#xff0c;可以确定软件能够正确选择和清理Mac垃圾文件&#xff0c;更加安全&#xff0c;可靠&#xff01;随着10周年纪念版的推出&#xff0c;CleanMyMac已经完成了向全面清理&…

MySQL的存储过程、存储函数和触发器

文章目录MySQL的存储过程什么是存储过程有哪些特性入门案例格式操作数据准备创建存储过程MySQL操作-变量定义局部变量格式操作用户变量格式操作系统变量介绍系统变量-全局变量格式操作系统变量-会话变量格式操作存储过程传参-in存储过程传参-out存储过程传参-inout存储过程传参…

Golang 操作TSV文件实战教程

本文介绍TSV文件类型及其应用&#xff0c;同时介绍Golang语句读取TSV文件并转为struct的实现过程。 认识TSV文件 也许你之前不了解TSV文件&#xff0c;无需担心&#xff0c;它很简单、很常用。TSV(tab-separated values)文件表示以tab分割值的文件格式&#xff0c;也就是说&am…

【Spring Boot】SpringBoot项目jar、war包启动解析

文章目录一、jar包和war包的区别1.1 war包1.2 jar包1.3 主要区别&#xff1a;二、SpringBoot使用war包启动2.1 Servlet3.0规范中引导应用启动的说明2.2 SpringBootServletInitializer的作用和原理三、SpringBoot使用jar包启动3.1 registerUrlProtocolHandler&#xff1a;注册UR…