HarmonyOS技术社区 · 2021年01月08日

#2020征文-手机#HarmonyOS轻量级偏好数据库初体验

这个数据库类似于NoSQL之类的数据库产品,存储结构是key-value

遇到问题

1 没有提供删除key的API,只有新增,查询

2 移除实例后还是可以添加,查询不知道是干啥的。

3 删除文件后还是可以添加,查询不知道是干啥的。

4 更改文件名称?这个API奇怪,说是更改文件路径但是targetFile不能输入路径。而且改了名称后也没啥作用

添加,查询也不影响。

<Text
    ohos:id="$+id:text_helloworld"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:background_element="$graphic:background_ability_main"
    ohos:layout_alignment="horizontal_center"
    ohos:text="Hello World"
    ohos:text_size="50"
     data-tomark-pass />

<TextField
    ohos:id="$+id:input"
    ohos:height="100vp"
    ohos:width="100vp"
    ohos:text_size="1px"></TextField>

<Button
    ohos:id="$+id:add"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="添加"
    ohos:text_size="100px"></Button>

<Button
    ohos:id="$+id:get"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="查询"
    ohos:text_size="100px"></Button>

<Button
    ohos:id="$+id:del"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="删除"
    ohos:text_size="100px"></Button>

<Button
    ohos:id="$+id:upd"
    ohos:height="match_content"
    ohos:width="match_content"
    ohos:text="更改"
    ohos:text_size="100px"></Button>package com.datang.myapplication.slice;

import com.datang.myapplication.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.TextField;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.orm.OrmPredicates;
import ohos.data.preferences.Preferences;
import ohos.data.rdb.ValuesBucket;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.Set;

//关系型数据库
public class MainAbilitySlice3 extends AbilitySlice {
final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x00201, "MY_TAG");
Preferences preferences = null;

@Override
public void onStart(Intent intent) {
    super.onStart(intent);
    super.setUIContent(ResourceTable.Layout_ability_main3);
    initDB();

    insert();
    select();
    delete();
    update();


    //监听无效
    preferences.registerObserver((p, k) -> {
        HiLog.info(label, "触发了监听,key为:%{public}s", k);
    });
}


@Override
public void onActive() {
    super.onActive();
}

public void initDB() {
    DatabaseHelper databaseHelper = new DatabaseHelper(this); // context入参类型为ohos.app.Context。
    String fileName = "test"; // fileName表示文件名,其取值不能为空,也不能包含路径,默认存储目录可以通过context.getPreferencesDir()获取。
    preferences = databaseHelper.getPreferences(fileName);
    File preferencesDir = this.getPreferencesDir();
    String absolutePath = preferencesDir.getAbsolutePath();
    HiLog.info(label, "默认路径%{public}s", absolutePath);
}

//增加
public void insert() {
    Button button = (Button) findComponentById(ResourceTable.Id_add);
    button.setClickedListener(e -> {//            preferences.putInt("intKey", 3);
//            preferences.flush();
//
//            preferences.putString("StringKey", "正经的value");
//            //同步刷新
//            preferences.flushSync();
//            //对同一个key重复添加会覆盖
//            preferences.putString("StringKey", "不正经的value");
//            //同步刷新
//            preferences.flushSync();
        TextField text = (TextField) findComponentById(ResourceTable.Id_input);
        String text1 = text.getText();
        preferences.putString(text1, text1 + "的value");
        preferences.flushSync();
        HiLog.info(label, "添加成功");
    });


}

//查询
public void select() {
    Button button2 = (Button) findComponentById(ResourceTable.Id_get);
    button2.setClickedListener(e -> {
        Map<String, ?> all = preferences.getAll();
        Set<String> keys = all.keySet();
        keys.forEach(c -> {
            HiLog.info(label, "获取到的key:%{public}s", c);
            String value = preferences.getString(c, "没有" + c + "这个key");
            HiLog.info(label, "key为:%{public}s,value为:%{public}s", c, value);
        });//            int anInt = preferences.getInt("intKey", -1);
//            HiLog.info(label, "获取到的key:%{public}d", anInt);
//
//            String string = preferences.getString("StringKey", "没有这个key");
//            HiLog.info(label, "获取到的key:%{public}s", string);
//
//            //当获取不到key时,则给出默认返回值,-1
//            int anInt2 = preferences.getInt("aaa", -1);
//            HiLog.info(label, "获取到的key:%{public}d", anInt2);
//
//            String string2 = preferences.getString("bbb", "没有这个key");
//            HiLog.info(label, "获取到的key:%{public}s", string2);
});
}
//删除
public void delete() {
    Button button3 = (Button) findComponentById(ResourceTable.Id_del);
    button3.setClickedListener(e -> {
        //这个移除实例后依然可以添加和查询,不知道是干啥的//            DatabaseHelper databaseHelper = new DatabaseHelper(this);
//            String fileName = "test"; // fileName表示文件名,其取值不能为空,也不能包含路径。
//            databaseHelper.removePreferencesFromCache(fileName);
//            HiLog.info(label, "移除实例");
        //删除文件后依然能新增和查看//            DatabaseHelper databaseHelper = new DatabaseHelper(this);
//            String fileName = "test"; // fileName表示文件名,其取值不能为空,也不能包含路径。
//            boolean result = databaseHelper.deletePreferences(fileName);
//            HiLog.info(label, "删除文件");
});
}
//更改
public void update() {
    Button button4 = (Button) findComponentById(ResourceTable.Id_upd);
    button4.setClickedListener(e -> {
        //这个更改也不知道干啥的//            DatabaseHelper databaseHelper = new DatabaseHelper(this);
//            String srcFile = "/data/data/com.datang.myapplication/MainAbility/preferences/test"; // srcFile表示源文件名或者源文件的绝对路径,不能为相对路径,其取值不能为空。当srcFile只传入文件名时,srcContext不能为空。
//            String targetFile = "test2"; // targetFile表示目标文件名,其取值不能为空,也不能包含路径。
//            Context srcContext = null;
//            boolean result = databaseHelper.movePreferences(null,srcFile,targetFile);
});
}

}

作者:顶风少年

想了解更多内容,请访问: 51CTO和华为官方战略合作共建的鸿蒙技术社区https://harmonyos.51cto.com/

推荐阅读
关注数
3035
内容数
446
华为鸿蒙相关技术,活动及资讯,欢迎关注及加入创作
目录
极术微信服务号
关注极术微信号
实时接收点赞提醒和评论通知
安谋科技学堂公众号
关注安谋科技学堂
实时获取安谋科技及 Arm 教学资源
安谋科技招聘公众号
关注安谋科技招聘
实时获取安谋科技中国职位信息