基于 OpenHarmony ActiveOhos_sqllite 组件开发指南

2024-04-07 22:44

本文主要是介绍基于 OpenHarmony ActiveOhos_sqllite 组件开发指南,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. ActiveOhos 功能介绍

1.1.组件介绍

基于鸿蒙系统连接数据库进行 sqlite 数据库操作的时候,创建连接的时候是有些繁琐的,本组件就是为了简化 sqlite 的连接,并且对鸿蒙原生的 API 进行封装加强,使得读写 sqlite 数据库的时候更加方便

1.2. phone 模拟器上运行效果

插入数据成功

2. ActiveOhos 使用方法

2.1.为应用添加 sqlitelibrary-debug.har 包依赖
在应用模块中调用 HAR,常用的添加依赖的方式包括如下两种。

方式一:依赖本地 HAR

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

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

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

  • 如果使用注解处理器的模块为“com.huawei.ohos.hap”模块,则需要在模块的 “build.gradle”文件的“ohos”节点中添加以下配置:
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 实现请求点击事件

<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<Button
ohos:id="$+id:btn_insert"
ohos:height="match_content"
ohos:width="80fp"
ohos:text_color="red"
ohos:text="插入"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_query"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="blue"
ohos:text="查询"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_update"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="green"
ohos:text="更新"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<Button
ohos:id="$+id:btn_delete"
ohos:height="match_content"
ohos:width="100fp"
ohos:text_color="black"
ohos:text="删除"
ohos:text_size="20fp"
ohos:weight="100fp"/>
<ListContainer
ohos:id="$+id:listText"
ohos:height="match_parent"
ohos:width="match_parent"/>
</DirectionalLayout>
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"
/>
</DirectionalLayout>

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.sqlitelibrary.DBManage;
import com.example.sqlitelibrary.DBOrmContext;
import com.example.sqlitelibrary.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<User> 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 方法

BookStore.java

@Database(entities = {User.class}, version = 1)
public abstract class BookStore extends OrmDatabase {
}

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

这篇关于基于 OpenHarmony ActiveOhos_sqllite 组件开发指南的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/883881

相关文章

Python ORM神器之SQLAlchemy基本使用完全指南

《PythonORM神器之SQLAlchemy基本使用完全指南》SQLAlchemy是Python主流ORM框架,通过对象化方式简化数据库操作,支持多数据库,提供引擎、会话、模型等核心组件,实现事务... 目录一、什么是SQLAlchemy?二、安装SQLAlchemy三、核心概念1. Engine(引擎)

Python自动化处理PDF文档的操作完整指南

《Python自动化处理PDF文档的操作完整指南》在办公自动化中,PDF文档处理是一项常见需求,本文将介绍如何使用Python实现PDF文档的自动化处理,感兴趣的小伙伴可以跟随小编一起学习一下... 目录使用pymupdf读写PDF文件基本概念安装pymupdf提取文本内容提取图像添加水印使用pdfplum

Maven中生命周期深度解析与实战指南

《Maven中生命周期深度解析与实战指南》这篇文章主要为大家详细介绍了Maven生命周期实战指南,包含核心概念、阶段详解、SpringBoot特化场景及企业级实践建议,希望对大家有一定的帮助... 目录一、Maven 生命周期哲学二、default生命周期核心阶段详解(高频使用)三、clean生命周期核心阶

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Nginx中配置使用非默认80端口进行服务的完整指南

《Nginx中配置使用非默认80端口进行服务的完整指南》在实际生产环境中,我们经常需要将Nginx配置在其他端口上运行,本文将详细介绍如何在Nginx中配置使用非默认端口进行服务,希望对大家有所帮助... 目录一、为什么需要使用非默认端口二、配置Nginx使用非默认端口的基本方法2.1 修改listen指令

从基础到进阶详解Python条件判断的实用指南

《从基础到进阶详解Python条件判断的实用指南》本文将通过15个实战案例,带你大家掌握条件判断的核心技巧,并从基础语法到高级应用一网打尽,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录​引言:条件判断为何如此重要一、基础语法:三行代码构建决策系统二、多条件分支:elif的魔法三、

基于Java开发一个极简版敏感词检测工具

《基于Java开发一个极简版敏感词检测工具》这篇文章主要为大家详细介绍了如何基于Java开发一个极简版敏感词检测工具,文中的示例代码简洁易懂,感兴趣的小伙伴可以跟随小编一起学习一下... 目录你是否还在为敏感词检测头疼一、极简版Java敏感词检测工具的3大核心优势1.1 优势1:DFA算法驱动,效率提升10

使用Python实现一个简易计算器的新手指南

《使用Python实现一个简易计算器的新手指南》计算器是编程入门的经典项目,它涵盖了变量、输入输出、条件判断等核心编程概念,通过这个小项目,可以快速掌握Python的基础语法,并为后续更复杂的项目打下... 目录准备工作基础概念解析分步实现计算器第一步:获取用户输入第二步:实现基本运算第三步:显示计算结果进