HarmonyOS应用API-轻量级数据库开发
- 调用相关的接口
import dataStorage from '@ohos.data.storage';
import featureAbility from '@ohos.ability.featureAbility'; // 用于获取文件存储路径
- 指定读写文件
var context = featureAbility.getContext();
context.getFilesDir().then((filePath) => {
console.info("======================>getFilesDirPromsie====================>"
let promise = dataStorage.getStorage(filePath + '/mystore');
});
- 储存数据
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.");
})
- 获取数据
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.")
})
- 删除数据
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);
})