HarmonyOS 鸿蒙Next 官方文档的关系型数据库存储的例子运行不出来?

HarmonyOS 鸿蒙Next 官方文档的关系型数据库存储的例子运行不出来? 我是按照这个做的。https://developer.harmonyos.com/cn/docs/documentation/doc-references-V3/js-apis-data-relationalstore-0000001493744128-V3#ZH-CN_TOPIC_0000001523648806__executesql

import relationalStore from '@ohos.data.relationalStore'; // 导入模块import featureAbility from '@ohos.ability.featureAbility';
@Entry
@Component
struct Index {
  @State message: string = 'Hello World'
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .height('100%')
  }
  onPageShow() {
    // 获取context
    let context = featureAbility.getContext();
    const STORE_CONFIG = {
      name: 'RdbTest.db', // 数据库文件名
      securityLevel: relationalStore.SecurityLevel.S1 // 数据库安全级别
    };
    const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT NOT NULL, AGE INTEGER, SALARY REAL, CODES BLOB)'; // 建表Sql语句
    relationalStore.getRdbStore(context, STORE_CONFIG, (err, store) => {
      if (err) {
        console.error(`Failed to get RdbStore. Code:${err.code}, message:${err.message}`);
        return;
      }
      console.info(`Succeeded in getting RdbStore.`);
      store.executeSql(SQL_CREATE_TABLE); // 创建数据表
      // 这里执行数据库的增、删、改、查等操作
      const valueBucket = {
        'NAME': 'Lisa',
        'AGE': 18,
        'SALARY': 100.5,
        'CODES': new Uint8Array([1, 2, 3, 4, 5])
      };
      store.insert('EMPLOYEE', valueBucket, (err, rowId) => {
        if (err) {
          console.error(`Failed to insert data. Code:${err.code}, message:${err.message}`);
          return;
        }
        console.info(`Succeeded in inserting data. rowId:${rowId}`);
      })
    });
  }
}

报错截图:

cke_4046.png


更多关于HarmonyOS 鸿蒙Next 官方文档的关系型数据库存储的例子运行不出来?的实战教程也可以访问 https://www.itying.com/category-93-b0.html

3 回复

开发者您好,你当前使用模拟器运行 还是真机运行?

更多关于HarmonyOS 鸿蒙Next 官方文档的关系型数据库存储的例子运行不出来?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


模拟器跑不了这个例子么?

09-09 23:10:49.511   4028-21988    A0c0d0/JSAPP                                          W     The getContext.getContext interface in the Previewer is a mocked implementation and may behave differently than on a real device.
09-09 23:10:49.511   4028-21988    A0c0d0/JSAPP                                          W     The BaseContext.constructor interface in the Previewer is a mocked implementation and may behave differently than on a real device.
09-09 23:10:49.511   4028-21988    A0c0d0/JSAPP                                          W     The Context.constructor interface in the Previewer is a mocked implementation and may behave differently than on a real device.
09-09 23:10:49.511   4028-21988    A0c0d0/JSAPP                                          W     The EventHub.constructor interface in the Previewer is a mocked implementation and may behave differently than on a real device.
09-09 23:10:49.511   4028-21988    A0c0d0/JSAPP                                          W     The relationalStore.getRdbStore interface in the Previewer is a mocked implementation and may behave differently than on a real device.
09-09 23:10:49.511   4028-21988    A0ff00/Rdb                                            E     [Debug.Rdb], gerRdbStore() failed, err: [object Object],

针对“HarmonyOS 鸿蒙Next 官方文档的关系型数据库存储的例子运行不出来”的问题,可能的原因及解决方向如下:

首先,确认是否严格按照官方文档的步骤进行操作。检查所有代码是否与文档一致,包括数据库配置、表结构定义、数据插入及查询语句等。

其次,检查开发环境是否满足要求。鸿蒙系统有其特定的开发环境和依赖库,确保所有必要的组件都已正确安装并配置。

再者,考虑数据库服务是否正常运行。如果是本地数据库,检查数据库服务是否启动;如果是远程数据库,检查网络连接及数据库服务状态。

此外,查看日志输出以获取错误信息。鸿蒙系统通常会有详细的日志记录功能,通过分析日志可以定位问题所在。

最后,确认鸿蒙系统版本是否与官方文档中的示例兼容。不同版本的鸿蒙系统可能在API和功能上存在差异。

如果经过上述步骤仍无法解决问题,可能是文档存在更新滞后或特定环境下的兼容性问题。此时,建议直接联系官网客服寻求帮助。官网地址是:https://www.itying.com/category-93-b0.html 如果问题依旧没法解决请联系官网客服。

回到顶部