当前位置:主页 > 查看内容

鸿蒙HarmonyOS三方件开发指南(6)-ActiveOhos_sqlite组件

发布时间:2021-06-30 00:00| 位朋友查看

简介:想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区 https://harmonyos.51cto.com/#zz 1. ActiveOhos功能介绍 1.1. 组件介绍 基于HarmonyOS据库进行sqlite数据库操作,创建连接时比较繁琐,本组件简化了sqlite数据库的连接,并且对Harmony……

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz

1. ActiveOhos功能介绍

1.1. 组件介绍

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

1.2. 手机模拟器上运行效果


插入数据成功


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”节点中添加以下配置:

  1. compileOptions{  
  2.  
  3.   annotationEnabled true 
  4.  

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

  1. dependencies {    compile files("orm_annotations_java.jar的路径 
  2. ","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")    
  3.  annotationProcessor files("orm_annotations_java.jar的路径 
  4. ","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")} 

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

  1. dependencies {    compile files("ohos.jar的路径","orm_annotations_java.jar的路径 
  2. ","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")          
  3. annotationProcessor files("orm_annotations_java.jar的路径 
  4. ","orm_annotations_processor_java.jar的路径","javapoet_java.jar的路径")} 

比如:


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

3. ActiveOhos开发实现

3.1. 主页面的布局文件

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

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.     xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.     ohos:height="match_parent" 
  5.     ohos:width="match_parent" 
  6.     ohos:orientation="vertical"
  7.  
  8.     <Button 
  9.         ohos:id="$+id:btn_insert" 
  10.         ohos:height="match_content" 
  11.         ohos:width="80fp" 
  12.         ohos:text_color="red" 
  13.         ohos:text="插入" 
  14.         ohos:text_size="20fp" 
  15.         ohos:weight="100fp"/> 
  16.  
  17.     <Button 
  18.         ohos:id="$+id:btn_query" 
  19.         ohos:height="match_content" 
  20.         ohos:width="100fp" 
  21.         ohos:text_color="blue" 
  22.         ohos:text="查询" 
  23.         ohos:text_size="20fp" 
  24.         ohos:weight="100fp"/> 
  25.  
  26.     <Button 
  27.         ohos:id="$+id:btn_update" 
  28.         ohos:height="match_content" 
  29.         ohos:width="100fp" 
  30.         ohos:text_color="green" 
  31.         ohos:text="更新" 
  32.         ohos:text_size="20fp" 
  33.         ohos:weight="100fp"/> 
  34.  
  35.     <Button 
  36.         ohos:id="$+id:btn_delete" 
  37.         ohos:height="match_content" 
  38.         ohos:width="100fp" 
  39.         ohos:text_color="black" 
  40.         ohos:text="删除" 
  41.         ohos:text_size="20fp" 
  42.         ohos:weight="100fp"/> 
  43.  
  44.     <ListContainer 
  45.         ohos:id="$+id:listText" 
  46.         ohos:height="match_parent" 
  47.         ohos:width="match_parent"/> 
  48.  
  49. </DirectionalLayout> 
  50.  
  51.  
  52.  
  53.         ohos:width="match_content" 
  54.  
  55.         ohos:background_element="$graphic:background_ability_main" 
  56.  
  57.         ohos:layout_alignment="horizontal_center" 
  58.  
  59.         ohos:text="get请求" 
  60.  
  61.         ohos:text_size="50" 
  62.  
  63.         ohos:top_margin="80vp" 
  64.  
  65.         /> 
  66.  
  67.  
  68.  
  69. </DirectionalLayout> 

3.2. 例子代码如下

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

  1. MainAbilitySlice 
  2.  
  3. import com.example.myapplication.BookStore; 
  4. import com.example.myapplication.ResourceTable; 
  5. import com.example.myapplication.User
  6. import com.example.sqlitelibrary.DBManage; 
  7. import com.example.sqlitelibrary.DBOrmContext; 
  8. import com.example.sqlitelibrary.utils.Log; 
  9. import ohos.aafwk.ability.AbilitySlice; 
  10. import ohos.aafwk.content.Intent; 
  11. import ohos.agp.components.Button; 
  12. import ohos.agp.components.Component; 
  13. import ohos.data.DatabaseHelper; 
  14. import ohos.data.orm.OrmContext; 
  15. import ohos.data.orm.OrmPredicates; 
  16. import ohos.data.rdb.RdbStore; 
  17. import ohos.data.rdb.ValuesBucket; 
  18.  
  19. import java.util.ArrayList; 
  20. import java.util.List; 
  21.  
  22. public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener { 
  23.      
  24.     private DatabaseHelper helper; 
  25.     private RdbStore store; 
  26.     private  OrmContext context; 
  27.     @Override 
  28.     public void onStart(Intent intent) { 
  29.         super.onStart(intent); 
  30.         super.setUIContent(ResourceTable.Layout_ability_main); 
  31.         helper = new DatabaseHelper(this); 
  32.         DBManage dbManger = new DBManage("user.db","user"); 
  33.         context = dbManger.getConnectionContext(helper, BookStore.class); 
  34. //         DBManage dbManger = new DBManage("user.db"); 
  35. //         store = dbManger.getConnectionStore(helper,"user"); 
  36.         Button btnInsert = (Button) findComponentById(ResourceTable.Id_btn_insert); 
  37.         Button btnQuery = (Button) findComponentById(ResourceTable.Id_btn_query); 
  38.         Button btnDelete = (Button) findComponentById(ResourceTable.Id_btn_delete); 
  39.         Button btnUpdate = (Button) findComponentById(ResourceTable.Id_btn_update); 
  40.         btnInsert.setClickedListener(this::onClick); 
  41.         btnQuery.setClickedListener(this::onClick); 
  42.         btnDelete.setClickedListener(this::onClick); 
  43.         btnUpdate.setClickedListener(this::onClick); 
  44.     } 
  45.  
  46.     @Override 
  47.     public void onActive() { 
  48.         super.onActive(); 
  49.     } 
  50.  
  51.     @Override 
  52.     public void onForeground(Intent intent) { 
  53.         super.onForeground(intent); 
  54.     } 
  55.  
  56.     @Override 
  57.     public void onClick(Component component) { 
  58. //        RdbStoreManage rdbStoreMange = new RdbStoreManage(); 
  59. //        ValuesBucket values = new ValuesBucket(); 
  60. //        values.putInteger("id", 1); 
  61. //        values.putString("name""zhangsan"); 
  62. //        values.putInteger("age", 18); 
  63. //        values.putDouble("salary", 100.5); 
  64. //        values.putByteArray("blobType", new byte[] {1, 2, 3}); 
  65. //        rdbStoreMange.setSql(store, "insert into user values(zhangsan, 18, 100.5, byte[1,2,3])"); 
  66. //        long id = rdbStoreMange.insert(store,"user"values); 
  67. //        System.out.println(id); 
  68.  
  69.         DBOrmContext dbOrmContext = new DBOrmContext(); 
  70.         switch (component.getId()) { 
  71.             case ResourceTable.Id_btn_insert: //插入数据 
  72.                 //第一次使用user对应的表的时候,如果有这张表就直接使用,没有就创建表 
  73.                 User user = new User(); 
  74.                 user.setFirstName("Zhang"); 
  75.                 user.setLastName("San"); 
  76.                 user.setAge(29); 
  77.                 user.setBalance(100.51); 
  78.                 boolean b = dbOrmContext.insert(context, user); 
  79.                 Log.i("插入成功"); 
  80.                 System.out.println(b); 
  81.                 break; 
  82.             case ResourceTable.Id_btn_query: //条件查询 
  83.                 List<User> users = new ArrayList<>(); 
  84.                 OrmPredicates query = context.where(User.class).equalTo("lastName""San"); 
  85.                 users = dbOrmContext.query(context, query); 
  86.                 break; 
  87.             case ResourceTable.Id_btn_delete: //条件删除 
  88.                 OrmPredicates delete = context.where(User.class).equalTo("lastName""San"); 
  89.                 int delete1 = dbOrmContext.delete(context, delete); 
  90.                 System.out.println(delete1); 
  91.                 break; 
  92.             case ResourceTable.Id_btn_update: //条件更新 
  93.                 ValuesBucket valuesBucket = new ValuesBucket(); 
  94.                 valuesBucket.putInteger("age", 31); 
  95.                 valuesBucket.putString("firstName""Zhang"); 
  96.                 valuesBucket.putString("lastName""San"); 
  97.                 valuesBucket.putDouble("balance", 300.51); 
  98.                 OrmPredicates update = context.where(User.class).equalTo("userId", 1); 
  99.                 int update1 = dbOrmContext.update(context, valuesBucket, update); 
  100.                 System.out.println(update1); 
  101.                 break; 
  102.         } 
  103.         dbOrmContext.flush(context); 
  104.     } 

user.java

  1. @Entity(tableName = "user", ignoredColumns = {"ignoreColumn1""ignoreColumn2"}, 
  2.         indices = {@Index(value = {"firstName""lastName"}, name = "name_index"unique = true)}) 
  3. public class User extends OrmObject { 
  4.     // 此处将userId设为了自增的主键。注意只有在数据类型为包装类型时,自增主键才能生效。 
  5.     @PrimaryKey(autoGenerate = true
  6.     private Integer userId; 
  7.     private String firstName; 
  8.     private String lastName; 
  9.     private int age; 
  10.     private double balance; 
  11.     private int ignoreColumn1; 
  12.     private int ignoreColumn2; 
  13.  
  14.     // 开发者自行添加字段的getter和setter 方法 

BookStore.java

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

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

欢迎交流:HWIS-HOS@isoftstone.com

©著作权归作者和HarmonyOS技术社区共同所有,如需转载,请注明出处,否则将追究法律责任

想了解更多内容,请访问:

51CTO和华为官方战略合作共建的鸿蒙技术社区

https://harmonyos.51cto.com/#zz


本文转载自网络,原文链接:https://harmonyos.51cto.com/#zz
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐