Rdb数据库存储,请问以下代码无报错,为什么不论调用几次,最终查询时结果都为空对象{} ?

const STORE_CONFIG: relationalStore.StoreConfig = { 
  name: "Rdbxxx.db", 
  securityLevel: relationalStore.SecurityLevel.S1 
}; 
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS WIKI (ID INTEGER PRIMARY KEY AUTOINCREMENT, localityData TEXT, localityDataTitle TEXT)'; 
relationalStore.getRdbStore(getContext(this), STORE_CONFIG, async (err, store) => { 
  if (err) { 
    console.error(`请求数据库错误. Code:${err.code}, message:${err.message}`); 
    return; 
  } 
 
  // 当数据库创建时,数据库默认版本为0 
  if (store.version === 0) { 
    store.executeSql(SQL_CREATE_TABLE); // 创建数据表 
    // 设置数据库的版本,入参为大于0的整数 
    store.version = 3; 
  } 
 
  // 如果数据库版本不为0且和当前数据库版本不匹配,需要进行升降级操作 
  // 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本 
  if (store.version === 1) { 
    // version = 1:表结构:WIKI (localityData, SALARY, CODES, ADDRESS) => version = 2:表结构:WIKI (localityData, localityDataTitle) 
    if (store !== undefined) { 
      (store as relationalStore.RdbStore).executeSql('ALTER TABLE WIKI ADD COLUMN AGE INTEGER'); 
      store.version = 2; 
    } 
  } 
 
  // 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本 
  if (store.version === 2) { 
    // version = 2:表结构:WIKI (localityData, localityDataTitle) => version = 3:表结构:WIKI (localityData, localityDataTitle) 
    if (store !== undefined) { 
      (store as relationalStore.RdbStore).executeSql('ALTER TABLE WIKI DROP COLUMN ADDRESS TEXT'); 
      store.version = 3; 
    } 
  } 
 
  store.insert("WIKI", { 
    "localityData": "JSON.stringify(this.data)", 
    "localityDataTitle": "JSON.stringify(this.dataTypeTitle)", 
  }, (err: BusinessError, rowId: number) => { 
    if (err) { 
      console.error(`请求数据库错误${err.code}, message:${err.message}`); 
      return; 
    } 
    console.info(`请求数据库rowId:${rowId}`); 
 
    store.query(new relationalStore.RdbPredicates("WIKI").notEqualTo("localityData", null), (err, resultSet) => { 
      if (err) { 
        console.info(`请求数据库错误:${err.code}, message:${err.message}`); 
        return; 
      } 
      console.info(`请求数据库`, JSON.stringify(resultSet)); 
    }) 
  } 
  ); 
});
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
HarmonyOS
2024-11-01 10:51:27
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

​.notEqualTo(“localityData”, null)

数据类型是string。null无法匹配建议换成

.notEqualTo(“localityData”, ‘’)

结果集resultSet需要您进行遍历 。无法直接转json。进行输出查看。

参考demo:​

import { relationalStore } from '@kit.ArkData'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
 
@Entry 
@Component 
struct Index { 
  @State message: string = 'Hello World'; 
 
  build() { 
    RelativeContainer() { 
      Text(this.message) 
        .id('HelloWorld') 
        .fontSize(50) 
        .fontWeight(FontWeight.Bold) 
        .alignRules({ 
          center: { anchor: '__container__', align: VerticalAlign.Center }, 
          middle: { anchor: '__container__', align: HorizontalAlign.Center } 
        }) 
        .onClick(()=>{ 
 
          const STORE_CONFIG: relationalStore.StoreConfig = { 
            name: "Rdbxxx.db", 
            securityLevel: relationalStore.SecurityLevel.S1 
          }; 
          const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS WIKI (ID INTEGER PRIMARY KEY AUTOINCREMENT, localityData TEXT, localityDataTitle TEXT)'; 
          relationalStore.getRdbStore(getContext(this), STORE_CONFIG, async (err, store) => { 
            if (err) { 
              console.error(`请求数据库错误. Code:${err.code}, message:${err.message}`); 
              return; 
            } 
 
            // 当数据库创建时,数据库默认版本为0 
            if (store.version === 0) { 
              store.executeSql(SQL_CREATE_TABLE); // 创建数据表 
              // 设置数据库的版本,入参为大于0的整数 
              store.version = 3; 
            } 
 
            // 如果数据库版本不为0且和当前数据库版本不匹配,需要进行升降级操作 
            // 当数据库存在并假定版本为1时,例应用从某一版本升级到当前版本,数据库需要从1版本升级到2版本 
            if (store.version === 1) { 
              // version = 1:表结构:WIKI (localityData, SALARY, CODES, ADDRESS) => version = 2:表结构:WIKI (localityData, localityDataTitle) 
              if (store !== undefined) { 
                (store as relationalStore.RdbStore).executeSql('ALTER TABLE WIKI ADD COLUMN AGE INTEGER'); 
                store.version = 2; 
              } 
            } 
 
            // 当数据库存在并假定版本为2时,例应用从某一版本升级到当前版本,数据库需要从2版本升级到3版本 
            if (store.version === 2) { 
              // version = 2:表结构:WIKI (localityData, localityDataTitle) => version = 3:表结构:WIKI (localityData, localityDataTitle) 
              if (store !== undefined) { 
                (store as relationalStore.RdbStore).executeSql('ALTER TABLE WIKI DROP COLUMN ADDRESS TEXT'); 
                store.version = 3; 
              } 
            } 
 
            store.insert("WIKI", { 
              "localityData": "JSON.stringify(this.data)", 
              "localityDataTitle": "JSON.stringify(this.dataTypeTitle)", 
            }, (err: BusinessError, rowId: number) => { 
              if (err) { 
                console.error(`请求数据库错误${err.code}, message:${err.message}`); 
                return; 
              } 
              console.info(`请求数据库rowId:${rowId}`); 
 
              store.query(new relationalStore.RdbPredicates("WIKI").notEqualTo("localityData", ''), (err, resultSet) => { 
                if (err) { 
                  console.info(`请求数据库错误:${err.code}, message:${err.message}`); 
                  return; 
                } 
                console.info(`ResultSet column names: ${resultSet.columnNames}, column count: ${resultSet.columnCount}`); 
                console.info('resultSet.goToNextRow()') 
                while (resultSet.goToNextRow()) { 
                  const localityData = resultSet.getString(resultSet.getColumnIndex('localityData')); 
                  const localityDataTitle = resultSet.getString(resultSet.getColumnIndex('localityDataTitle')); 
 
                  console.info(`id=${localityData}, name=${localityDataTitle}`); 
                } 
                // 释放数据集的内存 
                console.info(`请求数据库`, JSON.stringify(resultSet)); 
 
                resultSet.close(); 
 
              }) 
            } 
            ); 
          }); 
        }) 
    } 
    .height('100%') 
    .width('100%') 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
分享
微博
QQ
微信
回复
2024-11-01 17:37:30
相关问题
数据库存储的地址是什么
2807浏览 • 1回复 待解决
下代码报错什么原因
281浏览 • 0回复 待解决
以下代码导致程序崩溃了
1129浏览 • 1回复 待解决
HarmonyOS 如何选择数据库存储方案?
373浏览 • 0回复 待解决
下代码报错报错原因是什么
1245浏览 • 1回复 待解决
数据库存word怎么存?
4109浏览 • 1回复 待解决
首选项存储问题,为什么报错
1172浏览 • 1回复 待解决
鸿蒙的RDB数据库好用吗
3603浏览 • 2回复 已解决