onCreate() {
let context = this.context;
const kvManagerConfig: distributedKVStore.KVManagerConfig = {
context: context,
bundleName: ‘com.example.datamanagertest’
};
try {
// 创建KVManager实例
kvManager = distributedKVStore.createKVManager(kvManagerConfig);
console.info(‘Succeeded in creating KVManager.’);
// 继续创建获取数据库
} catch (e) {
let error = e as BusinessError;
console.error(Failed to create KVManager. Code:${error.code},message:${error.message}
);
}
}
}
if (kvManager !== undefined) {
kvManager = kvManager as distributedKVStore.KVManager;
//进行后续操作
//…
}
FA模型示例:
// 导入模块
import distributedKVStore from ‘@ohos.data.distributedKVStore’;
// FA模型
import featureAbility from ‘@ohos.ability.featureAbility’;
let kvManager: distributedKVStore.KVManager | undefined = undefined;
let context = featureAbility.getContext(); // 获取context
const kvManagerConfig: distributedKVStore.KVManagerConfig = {
context: context,
bundleName: ‘com.example.datamanagertest’
};
try {
kvManager = distributedKVStore.createKVManager(kvManagerConfig);
console.info(‘Succeeded in creating KVManager.’);
// 继续创建获取数据库
} catch (e) {
let error = e as BusinessError;
console.error(Failed to create KVManager. Code:${error.code},message:${error.message}
);
}
if (kvManager !== undefined) {
kvManager = kvManager as distributedKVStore.KVManager;
//进行后续操作
//…
}
2. 创建并获取键值数据库。示例代码如下所示:
let kvStore: distributedKVStore.SingleKVStore | undefined = undefined;
try {
const options: distributedKVStore.Options = {
createIfMissing: true,
encrypt: false,
backup: false,
autoSync: false,
// kvStoreType不填时,默认创建多设备协同数据库
kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
// 多设备协同数据库:kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION,
securityLevel: distributedKVStore.SecurityLevel.S1
};
kvManager.getKVStore<distributedKVStore.SingleKVStore>(‘storeId’, options, (err, store: distributedKVStore.SingleKVStore) => {
if (err) {
console.error(Failed to get KVStore: Code:${err.code},message:${err.message}
);
return;
}
console.info(‘Succeeded in getting KVStore.’);
kvStore = store;
// 请确保获取到键值数据库实例后,再进行相关数据操作
});
} catch (e) {
let error = e as BusinessError;
console.error(An unexpected error occurred. Code:${error.code},message:${error.message}
);
}
if (kvStore !== undefined) {
kvStore = kvStore as distributedKVStore.SingleKVStore;
//进行后续操作
//…
}
3. 调用put()方法向键值数据库中插入数据。示例代码如下所示:
const KEY_TEST_STRING_ELEMENT = ‘key_test_string’;
const VALUE_TEST_STRING_ELEMENT = ‘value_test_string’;
try {
kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => {
if (err !== undefined) {
console.error(Failed to put data. Code:${err.code},message:${err.message}
);
return;
}
console.info(‘Succeeded in putting data.’);
});
} catch (e) {
let error = e as BusinessError;
console.error(An unexpected error occurred. Code:${error.code},message:${error.message}
);
}
>
> **说明:**
>
>
> 当Key值存在时,put()方法会修改其值,否则新增一条数据。
>
>
>
4. 调用get()方法获取指定键的值。示例代码如下所示:
const KEY_TEST_STRING_ELEMENT = ‘key_test_string’;
const VALUE_TEST_STRING_ELEMENT = ‘value_test_string’;
try {
kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => {
if (err !== undefined) {
console.error(Failed to put data. Code:${err.code},message:${err.message}
);
return;
}
console.info(‘Succeeded in putting data.’);
kvStore = kvStore as distributedKVStore.SingleKVStore;
kvStore.get(KEY_TEST_STRING_ELEMENT, (err, data) => {
if (err != undefined) {
console.error(Failed to get data. Code:${err.code},message:${err.message}
);
return;
}
console.info(Succeeded in getting data. Data:${data}
);
});
});
} catch (e) {
let error = e as BusinessError;
console.error(Failed to get data. Code:${error.code},message:${error.message}
);
}
5. 调用delete()方法删除指定键值的数据。示例代码如下所示:
const KEY_TEST_STRING_ELEMENT = ‘key_test_string’;
const VALUE_TEST_STRING_ELEMENT = ‘value_test_string’;
try {
kvStore.put(KEY_TEST_STRING_ELEMENT, VALUE_TEST_STRING_ELEMENT, (err) => {
if (err !== undefined) {
console.error(Failed to put data. Code:${err.code},message:${err.message}
);
return;
}
console.info(‘Succeeded in putting data.’);
kvStore = kvStore as distributedKVStore.SingleKVStore;
kvStore.delete(KEY_TEST_STRING_ELEMENT, (err) => {
if (err !== undefined) {
console.error(Failed to delete data. Code:${err.code},message:${err.message}
);
return;
}
console.info(‘Succeeded in deleting data.’);
});
});
} catch (e) {
let error = e as BusinessError;
console.error(An unexpected error occurred. Code:${error.code},message:${error.message}
);
}
**为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:[`https://qr21.cn/FV7h05`]( )**
### 《鸿蒙开发学习手册》:
#### **如何快速入门:[`https://qr21.cn/FV7h05`]( )**
1. 基本概念
2. 构建第一个ArkTS应用
3. ……
![](https://img-blog.csdnimg.cn/img_convert/afd6b98a09014e557566dec0fd065c41.webp?x-oss-process=image/format,png)
#### **开发基础知识:[`https://qr21.cn/FV7h05`]( )**
1. 应用基础知识
2. 配置文件
3. 应用数据管理
4. 应用安全管理
5. 应用隐私保护
6. 三方应用调用管控机制
7. 资源分类与访问
8. 学习ArkTS语言
9. ……
![](https://img-blog.csdnimg.cn/img_convert/c02d1bcf9201e3d3d8baf3b812f8e370.webp?x-oss-process=image/format,png)
#### **基于ArkTS 开发:[`https://qr21.cn/FV7h05`]( )**
1. Ability开发
2. UI开发
3. 公共事件与通知
4. 窗口管理
5. 媒体
6. 安全
7. 网络与链接
8. 电话服务
**自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**
**深知大多数HarmonyOS鸿蒙开发工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
**因此收集整理了一份《2024年HarmonyOS鸿蒙开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。**
![img](https://img-blog.csdnimg.cn/img_convert/33f7a193e2c33f5f6d67f2f1a8023e37.png)
![img](https://img-blog.csdnimg.cn/img_convert/523aa05590ae100b299e3027680d493e.png)
![img](https://img-blog.csdnimg.cn/img_convert/488c09706f02b663cc788e0acb6852a3.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上HarmonyOS鸿蒙开发知识点,真正体系化!**
**由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
![img](https://img-blog.csdnimg.cn/img_convert/3254becff028f6af1d9b8a3a48eb6de5.png)
**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**
来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新**
**如果你觉得这些内容对你有帮助,可以添加VX:vip204888 (备注鸿蒙获取)**
[外链图片转存中...(img-8MQg03z0-1712844726139)]
**一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**