#鸿蒙学习大百科#Stage模型下如何获取键值型数据库的KVManager实例?

Stage模型下如何获取键值型数据库的KVManager实例?

HarmonyOS
2024-10-23 11:18:17
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
耗子煨汁r
// 导入模块
import distributedKVStore from '@ohos.data.distributedKVStore';
let kvManager: distributedKVStore.KVManager | undefined = undefined;
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
  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;
    //进行后续操作
    //...
  }
  
  }
分享
微博
QQ
微信
回复
2024-10-23 17:10:52
相关问题