[文章]HarmonyOS应用API-轻量级数据库开发

阅读量0
0
2
  1. 调用相关的接口

import dataStorage from '@ohos.data.storage';

import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径

  1. 指定读写文件

var context = featureAbility.getContext();

context.getFilesDir().then((filePath) => {

console.info("======================>getFilesDirPromsie====================>"

let promise = dataStorage.getStorage(filePath + '/mystore');

});

  1. 储存数据

promise.then((storage) => {

let getPromise = storage.put('startup', 'auto'); // 保存数据到缓存的storage实例中

getPromise.then(() => {

    console.info("Succeeded in putting the value of startup.");

}).catch((err) => {

    console.info("Failed to put the value of startup with err: " + err);

})

}).catch((err) => {

console.info("Failed to get the storage.");

})

  1. 获取数据

promise.then((storage) => {

let getPromise = storage.get('startup', 'default');

getPromise.then((value) => {

    console.info("The value of startup is " + value);

}).catch((err) => {

    console.info("Failed to get the value of startup with err: " + err);

})

}).catch((err) => {

console.info("Failed to get the storage.")

})

  1. 删除数据

let promise = dataStorage.deleteStorage(path + '/mystore');

promise.then(() => {

console.info("Succeeded in deleting the storage.");

}).catch((err) => {

console.info("Failed to delete the storage with err: " + err);

})

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友