今日看点

【软通动力】HarmonyOS三方件开发指南(6)-ActiveOhos_sqlite组件

发表于话题:HarmonyOS 2
发布时间:2021-06-02

标签:sqlite java ActiveOhos text jar HarmonyOS user ohos import

1.    ActiveOhos功能介绍

1.1. 组件介绍

       基于HarmonyOS据库进行sqlite数据库操作,创建连接时比较繁琐,本组件简化了sqlite数据库的连接,并且对HarmonyOS原生的API进行封装加强,使sqlite数据库的读写更加方便。
1.2. 手机模拟器上运行效果

  

插入数据成功

2.    ActiveOhos使用方法

 2.1. 为应用添加s-debug.har包依赖

在应用模块中调用HAR,常用的添加依赖为:依赖本地HAR

第一步:将s-debug.har复制到entry\libs目录下即可(由于build.gradle中已经依赖的libs目录下的*.har,因此不需要再做修改)。

查看工程目录中build.gradle下的*.har是否存在

第二步:除了依赖har之外还需要添加外部依赖用来实现类的引入,引入方式如下,引入完之后同步即可使用。

如果使用注解处理器的模块为“com.huawei.ohos.hap”,则需要在模块 “build.gradle”文件的“ohos”节点中添加以下配置:

1 2 3 compileOptions{    annotationEnabled true }

如果使用注解处理器的模块为“com.huawei.ohos.library”,则需要在模块“build.gradle”文件的“dependencies”节点中配置注解处理器。查看“orm_annotations_java.jar”、“orm_annotations_processor_java.jar” 、“javapoet_java.jar” 3个jar包在HUAWEI SDK中的对应目录,并将这三个jar包导入项目中。

dependencies {    compile files("orm_annotations_java.jar的路径

,orm_annotations_processor_java.jar的路径,javapoet_java.jar的路径)   

 annotationProcessor files("orm_annotations_java.jar的路径

,orm_annotations_processor_java.jar的路径,javapoet_java.jar的路径)}

如果使用注解处理器的模块为“java-library”,则需要在模块 “build.gradle”文件的“dependencies”节点中配置注解处理器,并导入“ohos.jar”。

dependencies {    compile files("ohos.jar的路径","orm_annotations_java.jar的路径

","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")         

annotationProcessor files("orm_annotations_java.jar的路径

","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")}

比如:

以上操作无误 之后就可以进行编码了!

3.    ActiveOhos开发实现

3.1. 主页面的布局文件

定义四个按钮分别实现增删改查,定义四个Button实现请求点击事件

   

   

   

   

   

        ohos:width="match_content"

        ohos:background_element="$graphic:background_ability_main"

        ohos:layout_alignment="horizontal_center"

        ohos:text="get请求"

        ohos:text_size="50"

        ohos:top_margin="80vp"

        />

3.2. 例子代码如下

        组件中有两种连接数据的方式,分别是OrmContext,RdbStore ,其中使用OrmContext连接方式时,需要定义一个实体类(User)来和数据库对应表名及字段,一个数据库类 BookStore 来配合开发,代码如下:

MainAbilitySlice

import com.example.myapplication.BookStore;

import com.example.myapplication.ResourceTable;

import com.example.myapplication.User;

import com.example.s.DBManage;

import com.example.s.DBOrmContext;

import com.example.s.utils.Log;

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

import ohos.agp.components.Button;

import ohos.agp.components.Component;

import ohos.data.DatabaseHelper;

import ohos.data.orm.OrmContext;

import ohos.data.orm.OrmPredicates;

import ohos.data.rdb.RdbStore;

import ohos.data.rdb.ValuesBucket;

import java.util.ArrayList;

import java.util.List;

public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private DatabaseHelper helper;

    private RdbStore store;

    private  OrmContext context;

    @Override

    public void onStart(Intent intent) {

        super.onStart(intent);

        super.setUIContent(ResourceTable.Layout_ability_main);

        helper = new DatabaseHelper(this);

        DBManage dbManger = new DBManage("user.db","user");

        context = dbManger.getConnectionContext(helper, BookStore.class);

//         DBManage dbManger = new DBManage("user.db");

//         store = dbManger.getConnectionStore(helper,"user");

        Button btnInsert = (Button) findComponentById(ResourceTable.Id_btn_insert);

        Button btnQuery = (Button) findComponentById(ResourceTable.Id_btn_query);

        Button btnDelete = (Button) findComponentById(ResourceTable.Id_btn_delete);

        Button btnUpdate = (Button) findComponentById(ResourceTable.Id_btn_update);

        btnInsert.setClickedListener(this::onClick);

        btnQuery.setClickedListener(this::onClick);

        btnDelete.setClickedListener(this::onClick);

        btnUpdate.setClickedListener(this::onClick);

    }

    @Override

    public void onActive() {

        super.onActive();

    }

    @Override

    public void onForeground(Intent intent) {

        super.onForeground(intent);

    }

    @Override

    public void onClick(Component component) {

//        RdbStoreManage rdbStoreMange = new RdbStoreManage();

//        ValuesBucket values = new ValuesBucket();

//        values.putInteger("id", 1);

//        values.putString("name", "zhangsan");

//        values.putInteger("age", 18);

//        values.putDouble("salary", 100.5);

//        values.putByteArray("blobType", new byte[] {1, 2, 3});

//        rdbStoreMange.setSql(store, "insert into user values(zhangsan, 18, 100.5, byte[1,2,3])");

//        long id = rdbStoreMange.insert(store,"user", values);

//        System.out.println(id);

        DBOrmContext dbOrmContext = new DBOrmContext();

        switch (component.getId()) {

            case ResourceTable.Id_btn_insert: //插入数据

                //第一次使用user对应的表的时候,如果有这张表就直接使用,没有就创建表

                User user = new User();

                user.setFirstName("Zhang");

                user.setLastName("San");

                user.setAge(29);

                user.setBalance(100.51);

                boolean b = dbOrmContext.insert(context, user);

                Log.i("插入成功");

                System.out.println(b);

                break;

            case ResourceTable.Id_btn_query: //条件查询

                List users = new ArrayList();

                OrmPredicates query = context.where(User.class).equalTo("lastName", "San");

                users = dbOrmContext.query(context, query);

                break;

            case ResourceTable.Id_btn_delete: //条件删除

                OrmPredicates delete = context.where(User.class).equalTo("lastName", "San");

                int delete1 = dbOrmContext.delete(context, delete);

                System.out.println(delete1);

                break;

            case ResourceTable.Id_btn_update: //条件更新

                ValuesBucket valuesBucket = new ValuesBucket();

                valuesBucket.putInteger("age", 31);

                valuesBucket.putString("firstName", "Zhang");

                valuesBucket.putString("lastName", "San");

                valuesBucket.putDouble("balance", 300.51);

                OrmPredicates update = context.where(User.class).equalTo("userId", 1);

                int update1 = dbOrmContext.update(context, valuesBucket, update);

                System.out.println(update1);

                break;

        }

        dbOrmContext.flush(context);

    }

}

user.java

@Entity(tableName = "user", ignoredColumns = {"ignoreColumn1", "ignoreColumn2"},

        indices = {@Index(value = {"firstName", "lastName"}, name = "name_index", unique = true)})

public class User extends OrmObject {

    // 此处将userId设为了自增的主键。注意只有在数据类型为包装类型时,自增主键才能生效。

    @PrimaryKey(autoGenerate = true)

    private Integer userId;

    private String firstName;

    private String lastName;

    private int age;

    private double balance;

    private int ignoreColumn1;

    private int ignoreColumn2;

    // 开发者自行添加字段的getter和setter 方法

项目源代码地址:https://github.com/isoftstone-dev/Active_HarmonyOS

欢迎交流:

 

原文链接:https://developer.huawei.com/consumer/cn/forum/topic/0202558662106980609?fid=0101303901040230869

原作者:软通动力HOS

标签:sqlite,java,ActiveOhos,text,jar,HarmonyOS,user,ohos,import 来源: https://www.cnblogs.com/developer-huawei/p/14809108.html

标签组:[软通动力

本文来源:https://www.kandian5.com/articles/24015.html

相关阅读

宋理宗聪明绝顶为什么选了一个白痴当太子

赵禥是南宋第六位皇帝,即宋度宗,他在位十年,毫无作为,整日吃喝玩乐,于1274年病死,他虽然不是南宋的亡国之君,但却是南宋亡国的罪人!更让人意想不到的是,赵禥是个弱智!根据史书记载,赵禥的父亲是荣王赵...

2025-09-12

特洛伊战争为什么要围城九年

英雄们聚集在奥利斯港湾(Aulis),军队人数有十万人,船数一千一百八十六。出发前大家都在岸边祭坛作献祭,忽然间祭坛下面爬出了一条血红的怪蛇,它弯曲成环状爬上了树,爬到树最高处的一个鸟巢,吃了一只雌鸟...

2025-09-12

鱼玄机是被丈夫骗进道观的吗

鱼玄机出生在长安,当时的大唐国都。生逢盛世,但是家世早衰,鱼玄机出生时,家境已经十分贫寒。她的父亲虽然读过几年书,腹中有些墨水,但是仕途很不顺遂,只能将自己的全部期望寄托在女儿身上。当时鱼玄机还叫做鱼...

2025-09-12

特洛伊战争一共持续几年

特洛伊战争是发生在古希腊的一次战争,发生的地点是现在地中海附近。这场战争的导火索普遍认为是美女海伦,为了抢夺一个名叫海伦的绝世美女,斯巴达王与帕里斯短兵相接。但是有学者推测,这场战事其实是一场盟军对抗...

2025-09-12

荸荠不能生吃吗

姜片虫感染还是挺麻烦的,轻则肠炎腹泻,重则贫血浮肿,儿童可能出现智力低下,甚至死亡。姜片虫在水生植物上都可能存在,不光荸荠,什么茭白菱角,乃至空心菜等,都要反复清洗,保证做熟再吃。尤其荸荠和菱角,千万...

2025-09-12

西晋公主流落街头被卖去当丫鬟下场是什么

这位公主就是西晋时期的临海公主,她的父亲是司马衷,她出生的时候,八王之乱刚刚结束不久,作为公主原本应该享受富贵的生活,可是没过多久西晋就被灭了。皇室成员纷纷外逃,为了保住性命她也逃出了洛阳。在逃跑的途...

2025-09-12

最惨的和亲公主10岁出嫁三个月后离奇身亡

虽然历史上和亲的公主有许多是宫女顶替的,但送去之后多数的结局并不好,而且有许多年幼的公主,尚未涉足人世间的险恶,就不得不去独自面对他乡的险恶,在历史上曾有一位年仅10岁的公主和亲,可因为生得太美,便被...

2025-09-12

古代被抄家的女子是什么下场

《绣春刀》里所说的,入教坊司当妓女只是其中一种结果,但是,电影中所讲的不一定确切,入了教坊司不一定做妓女。教坊司是干什么的呢?主要负责"朝会宴享,冬至,正旦,进实录,册封,诏进士,告祖庙,祭郊祀,进士...

2025-09-12

晋孝武帝真的因为嘲笑张贵人人老色衰被妃子杀了吗

这一天,晋孝武帝和张贵人一起饮酒作乐,晋孝武帝让张贵人多喝几杯,而张贵人已经喝够了,所以就不喝了,晋孝武帝就跟她开玩笑说:“你都年过三十了,人老色衰了,后宫里比你年轻的美女多着呢,信不信我废掉你,把你...

2025-09-12

特洛伊战争的结局究竟是怎样的

特洛伊战争结局以特洛伊城池化为一片废墟而告终。由于特洛伊城池在宙斯的两个儿子修建下非常牢固,所以易守难攻,迈锡尼士兵攻占十年也未能攻下城池,最终伟大的英雄奥德赛献计,伪装成军队已经离开的样子,让迈锡尼...

2025-09-12