如何读取本地/预制数据库
============

**解决措施**

1.  将db文件推送到数据存储沙箱路径：/data/app/el2/100/database/(bundleName)/entry/rdb/。实现方式为使用文件管理接口打开本地数据库，读取其内容并写入沙箱路径下的db文件中。
    
    import { fileIo } from '@kit.CoreFileKit';
    import { relationalStore } from '@kit.ArkData';
    import { common } from '@kit.AbilityKit';
    import { BusinessError } from '@kit.BasicServicesKit';
    
    _// Obtaining the Context in EntryAbility, save it to AppStorage, then use AppStorage to retrieve it in the utility class._
    let context = AppStorage.get('context') as UIContext;
    let UiAbilityContent = context.getHostContext() as common.UIAbilityContext;
    let RDBDirectory = UiAbilityContent.databaseDir;
    let resource = UiAbilityContent.resourceManager;
    
    function initDatabase() {
     _ // Create a database sandbox directory_
      try {
        let dirPath = RDBDirectory + '/rdb';
        fileIo.mkdirSync(dirPath);
      } catch (error) {
        console.error(\`mkdir rdbPath failed, error code: ${error.code}, message: ${error.message}.\`);
      }
    
     _ // Set db name_
      let dbName: string = 'Objective.db';
    
     _ // Read the db file in the rawfile directory_
      try {
        let content = resource.getRawFileContentSync(dbName);
        let cFile = RDBDirectory + '/rdb/' + dbName;
        let cacheFile = fileIo.openSync(cFile, fileIo.OpenMode.READ\_WRITE | fileIo.OpenMode.CREATE);
        fileIo.write(cacheFile.fd, content.buffer);
        fileIo.closeSync(cacheFile.fd);
      } catch (error) {
        console.error(\`callback getRawFd failed, error code: ${error.code}, message: ${error.message}.\`);
      }
    }
    
2.  通过[getRdbStore](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/arkts-apis-data-relationalstore-f#relationalstoregetrdbstore)获取保存在沙箱路径下的db文件
    
    async function getRDB(): Promise<relationalStore.RdbStore | undefined> {
      let result: relationalStore.RdbStore | undefined = undefined;
      const STORE\_CONFIG: relationalStore.StoreConfig = {
        name: 'Objective.db',
        securityLevel: relationalStore.SecurityLevel.S1
      };
    
      await relationalStore.getRdbStore(UiAbilityContent, STORE\_CONFIG).then((rdbStore: relationalStore.RdbStore) => {
        result = rdbStore;
        console.info('Get RdbStore successfully.');
      }).catch((err: BusinessError) => {
        console.error(\`Get RdbStore failed, code is ${err.code}, message is ${err.message}\`);
      });
      return result;
    }