1. 程式人生 > 實用技巧 >#2020徵文-手機#HarmonyOS輕量級偏好資料庫初體驗

#2020徵文-手機#HarmonyOS輕量級偏好資料庫初體驗

這個資料庫類似於NoSQL之類的資料庫產品,儲存結構是key-value

遇到問題

1 沒有提供刪除key的API,只有新增,查詢

2 移除例項後還是可以新增,查詢不知道是幹啥的。

3 刪除檔案後還是可以新增,查詢不知道是幹啥的。

4 更改檔名稱?這個API奇怪,說是更改檔案路徑但是targetFile不能輸入路徑。而且改了名稱後也沒啥作用

新增,查詢也不影響。

<?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"> <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" /> <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> </DirectionalLayout> 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/