#打卡不停更#HarmonyOS应用API-轻量级数据库开发-鸿蒙开发者社区-51CTO.COM

#打卡不停更#HarmonyOS应用API-轻量级数据库开发

鸿蒙时代
发布于 2022-10-15 15:39
浏览
0收藏

1.调用相关的接口
import dataStorage from ‘@ohos.data.storage’;
import featureAbility from ‘@ohos.ability.featureAbility’; // 用于获取文件存储路径

2.指定读写文件
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
console.info(“======================>getFilesDirPromsie====================>”
let promise = dataStorage.getStorage(filePath + ‘/mystore’);
});

3.储存数据
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.”);
})

4.获取数据
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.”)
})

5.删除数据
let promise = dataStorage.deleteStorage(path + ‘/mystore’);
promise.then(() => {
console.info(“Succeeded in deleting the storage.”);
}).catch((err) => {

标签
HarmonyOS应用API-轻量级数据库开发.docx 17.84K 15次下载
收藏
回复
举报
回复
    相关推荐