HarmonyOS 数据库查询是否支持位运算

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
shlp

数据库查询支持位运算,参考示例:

import relationalStore from '@ohos.data.relationalStore';
import { BusinessError } from '@kit.BasicServicesKit';

const SQL_CREATE_TABLE =
  'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT NOT NULL,AGE INTEGER,SALARY INTEGER)';

function valuesTest() {
  let valueBucketArray: Array<relationalStore.ValuesBucket> = new Array();

  for (let i = 0; i < 10; i++) {
    const valueBucket: relationalStore.ValuesBucket = {
      "name": "xbx",
      "age": 24,
      "salary": 0,
    }
    valueBucketArray.push(valueBucket);
  }
  console.log('valueBucketArray ' + JSON.stringify(valueBucketArray))
  return valueBucketArray
}


@Entry
@Component
struct Index {
  @State message: string = 'Hello World';
  rdbStore: relationalStore.RdbStore | undefined = undefined
  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(async () => {
          const STORE_CONFIG: relationalStore.StoreConfig = {
            name: 'RdbTest3.db', // 数据库文件名
            securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别
            encrypt: false, // 可选参数,指定数据库是否加密,默认不加密

            allowRebuild: true
          };
          let RdbStore = await relationalStore.getRdbStore(getContext(), STORE_CONFIG)
          this.rdbStore = RdbStore
          console.log("Succeeded in getting RdbStore.")
          await RdbStore.executeSql(SQL_CREATE_TABLE)
          console.log("建表成功")
          try {
            RdbStore.batchInsert("EMPLOYEE", valuesTest());
            console.info(`Insert data successfully!`);
          } catch (err) {
            console.error(`Insert datae failed! err code:${err.code}, err message:${err.message}`)
          }
        })

      Button('query').onClick(async () => {

        console.info("rebuilt " + this.rdbStore?.rebuilt)

        let res = this.rdbStore?.querySqlSync('SELECT ID,NAME,AGE,SALARY FROM EMPLOYEE')
        console.info(`querySqlSync data successfully! row count:${res?.rowCount}`);
        while (res?.goToNextRow()) {
          const id = res?.getLong(res?.getColumnIndex("ID"));

          const name = res?.getString(res?.getColumnIndex("NAME"));

          const age = res?.getLong(res?.getColumnIndex("AGE"));

          const salary = res?.getDouble(res?.getColumnIndex("SALARY"));

          console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
        }
        // 释放数据集的内存
        res?.close();
      })

      Button('UPDATE')
        .margin({ top: 40 })
        .onClick(async () => {
          this.rdbStore?.executeSql('UPDATE EMPLOYEE SET AGE = SALARY', (err, data) => {
            if (err) {
              console.error(`executeSql error ${err.code} message ${err.message}`);
            }
            console.info(`executeSql data successfully`);
          })

        })

      Button('query1')
        .margin({ top: 100 })
        .onClick(async () => {
          await this.rdbStore?.querySql('SELECT * FROM EMPLOYEE WHERE ID = 1 AND (SALARY & 0) = 0').then((data: relationalStore.ResultSet) => {
            console.info('execute data= ' + data.rowCount)
            while (data?.goToNextRow()) {
              const id = data?.getLong(data.getColumnIndex("ID"));
              const name = data?.getString(data.getColumnIndex("NAME"));
              const age = data?.getLong(data.getColumnIndex("AGE"));
              const salary = data.getDouble(data.getColumnIndex("SALARY"));
              console.info(`id=${id}, name=${name}, age=${age}, salary=${salary}`);
            }
            // 释放数据集的内存
            data.close();
          }).catch((error: BusinessError) => {
            console.error("ERROR= " + JSON.stringify(error))
          })
        })
    }
    .height('100%')
    .width('100%')
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS 运算问题
144浏览 • 1回复 待解决
HarmonyOS 有关运算异常
155浏览 • 2回复 待解决
用户查询消息数据库设计
3230浏览 • 1回复 待解决
后端查询数据库的性能问题
4060浏览 • 1回复 待解决
关系型数据库查询问题
583浏览 • 1回复 待解决
如何对非关系数据库进行查询
2026浏览 • 1回复 待解决
数据库多表关联查询有了解的吗?
2268浏览 • 1回复 待解决
HarmonyOS数据库数据变化是否可以监听
686浏览 • 1回复 待解决
数据库查询问题,有没有知道的?
387浏览 • 1回复 待解决
sql数据库查询数据量太大查不出来?
4032浏览 • 1回复 待解决
wgcloud支持使用postgresql做数据库
4160浏览 • 1回复 待解决