【鸿蒙】单版本分布式数据库实战

简介: 周末如期而至,学习也不能停止,分布式数据库实战搞起!1).要使用分布式的化首先就得打开权限,在config.json中添加permisssion权限。

周末如期而至,学习也不能停止,分布式数据库实战搞起!

1).要使用分布式的化首先就得打开权限,在config.json中添加permisssion权限。

"reqPermissions": [{
      "name": "ohos.permission.DISTRIBUTED_DATASYNC"
    }
    ],

这段代码添加在abilities同一目录层级

2).再将权限调用放在onstart方法里面


requestPermissionsFromUser(new String[]{"ohos.permission.DISTRIBUTED_DATASYNC"},0);

1.UI布局

默认文件ability_main的xml代码,五个按钮

<?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:background_element="#FF34D2BA"
    ohos:orientation="vertical">
    <DependentLayout
        ohos:height="50vp"
        ohos:width="match_parent"
        ohos:left_margin="30vp"
        ohos:right_margin="30vp"
        ohos:top_margin="30vp"
        ohos:background_element="$graphic:background">
        <Button
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:id="$+id:write_button"
            ohos:text="分布式写数据"
            ohos:text_size="20vp"
            ohos:text_color="#9b9b9b"
            ohos:top_padding="10vp"
            />
    </DependentLayout>
    <DependentLayout
    ohos:height="50vp"
    ohos:width="match_parent"
    ohos:left_margin="30vp"
    ohos:right_margin="30vp"
    ohos:top_margin="30vp"
    ohos:background_element="$graphic:background">
    <Button
        ohos:height="match_content"
        ohos:width="match_parent"
        ohos:id="$+id:delete_button"
        ohos:text="分布式删除数据"
        ohos:text_color="#9b9b9b"
        ohos:text_size="20vp"
        ohos:top_padding="10vp"
        />
    </DependentLayout>
    <DependentLayout
    ohos:height="50vp"
    ohos:width="match_parent"
    ohos:left_margin="30vp"
    ohos:right_margin="30vp"
    ohos:top_margin="30vp"
    ohos:background_element="$graphic:background">
    <Button
    ohos:height="match_content"
    ohos:width="match_parent"
    ohos:id="$+id:read_button"
    ohos:text="分布式读取数据"
    ohos:text_color="#9b9b9b"
    ohos:text_size="20vp"
    ohos:top_padding="10vp"
        />
    </DependentLayout>
    <DependentLayout
        ohos:height="50vp"
        ohos:width="match_parent"
        ohos:left_margin="30vp"
        ohos:right_margin="30vp"
        ohos:top_margin="30vp"
        ohos:background_element="$graphic:background">
        <Button
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:id="$+id:close_button"
            ohos:text="分布式关闭数据"
            ohos:text_color="#9b9b9b"
            ohos:text_size="20vp"
            ohos:top_padding="10vp"
            />
    </DependentLayout>
    <DependentLayout
        ohos:height="50vp"
        ohos:width="match_parent"
        ohos:left_margin="30vp"
        ohos:right_margin="30vp"
        ohos:top_margin="30vp"
        ohos:background_element="$graphic:background">
        <Button
            ohos:height="match_content"
            ohos:width="match_parent"
            ohos:id="$+id:del_button"
            ohos:text="删除分布式数据库"
            ohos:text_color="#9b9b9b"
            ohos:text_size="20vp"
            ohos:top_padding="10vp"
            />
    </DependentLayout>
</DirectionalLayout>

包括按钮的形状

<?xml version="1.0" encoding="UTF-8" ?>
<shape xmlns:ohos="http://schemas.huawei.com/res/ohos"
       ohos:shape="rectangle">
    <solid ohos:color="#ffffff"/>
    <stroke ohos:width="3vp"
            ohos:color="#ffffff"
            />
    <corners ohos:radius="20vp"/>
</shape>


2.注入灵魂

Ctrl c+v一顿操作




我们可以看一看options当中的默认值



然后随便打印几个出来


接下来就自定义,有的不需要默认值,我们就修改

 options.setCreateIfMissing(true)
         .setEncrypt(false)
         .setKvStoreType(KvStoreType.SINGLE_VERSION);

响应代码

public void onClick(Component component) {
            int componentId = component.getId();
            switch (componentId) {
                case ResourceTable.Id_write_button: {
                    //写入数据
                    try {
                        singleKvStore.putString("test", "wo ai huawei");
                        System.out.println("======写入数据");
                        Tools.showTip(MainAbilitySlice.this,"写入数据成功");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                case ResourceTable.Id_del_button: {
                    //删除数据
                    try {
                        singleKvStore.delete("test");
                        System.out.println("======删除数据");
                        Tools.showTip(MainAbilitySlice.this,"写入数据成功");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                case ResourceTable.Id_read_button: {
                    //读出数据
                    try {
                        singleKvStore.getString("test");
                        System.out.println("======读取数据");
                        Tools.showTip(MainAbilitySlice.this,"读取数据成功");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                case ResourceTable.Id_close_button: {
                    //关闭数据库
                    try {
                        kvManager.closeKvStore(singleKvStore);
                        System.out.println("======关闭数据库");
                        Tools.showTip(MainAbilitySlice.this,"关闭数据库成功");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                case ResourceTable.Id_delete_button: {
                    //删除数据库
                    try {
                        kvManager.deleteKvStore(stroeId);
                        System.out.println("======删除数据库");
                        Tools.showTip(MainAbilitySlice.this,"删除数据库成功");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
                break;
                default:
                    break;
            }
        }

启动第一部


启动第二部




动图演示,在一台设备写入数据,另一台能够读取,删除则无法读取


9e28e27444674d69a72b50ebd4357582.gif

相关文章
|
6天前
|
关系型数据库 MySQL 分布式数据库
《MySQL 简易速速上手小册》第6章:MySQL 复制和分布式数据库(2024 最新版)
《MySQL 简易速速上手小册》第6章:MySQL 复制和分布式数据库(2024 最新版)
36 2
|
1月前
|
数据库
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
ABAP 泛型编程实战 - 分享一个数据库表内容的拷贝工具试读版
20 0
|
1月前
|
Oracle 关系型数据库 分布式数据库
分布式数据库集成解决方案
分布式数据库集成解决方案
204 0
|
2月前
|
消息中间件 RocketMQ 微服务
RocketMQ 分布式事务消息实战指南
RocketMQ 分布式事务消息实战指南
266 1
|
26天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
96 0
|
2月前
|
Java 数据库连接 API
分布式事物【XA强一致性分布式事务实战、Seata提供XA模式实现分布式事务】(五)-全面详解(学习总结---从入门到深化)
分布式事物【XA强一致性分布式事务实战、Seata提供XA模式实现分布式事务】(五)-全面详解(学习总结---从入门到深化)
59 0
|
1天前
|
存储 分布式计算 Hadoop
基于Hadoop分布式数据库HBase1.0部署及使用
基于Hadoop分布式数据库HBase1.0部署及使用
|
5天前
|
SQL 关系型数据库 MySQL
Python与MySQL数据库交互:面试实战
【4月更文挑战第16天】本文介绍了Python与MySQL交互的面试重点,包括使用`mysql-connector-python`或`pymysql`连接数据库、执行SQL查询、异常处理、防止SQL注入、事务管理和ORM框架。易错点包括忘记关闭连接、忽视异常处理、硬编码SQL、忽略事务及过度依赖低效查询。通过理解这些问题和提供策略,可提升面试表现。
25 6
|
10天前
|
SQL 数据库
数据库SQL语言实战(二)
数据库SQL语言实战(二)
|
26天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)(一)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)
30 0

热门文章

最新文章