HarmonyOS 批量插入的操作使用 taskpool 变成 Concurrent 可以嘛

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

批量插入操作可以使用 taskpool 变成 Concurrent

index.ets

import { common } from '@kit.AbilityKit';
import { relationalStore } from '@kit.ArkData';
import { taskpool } from '@kit.ArkTS';
import RdbTest from './Rdb';

@Concurrent
function batchInsert(context: common.Context, valueBuckets: Array<relationalStore.ValuesBucket>) {
  console.info('run - concurrent batchInsert()')
  try {
    new RdbTest().batchInsert(context, valueBuckets)
  } catch (err) {
    console.error('err - concurrent batchInsert error ' + err.message)
  }
  console.info('run end - concurrent batchInsert()')
}


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

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


@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(() => {
          taskpool.execute(batchInsert, getContext(), valuesTest())
        })

      Button('query').onClick(async () => {
        const STORE_CONFIG: relationalStore.StoreConfig = {
          name: 'RdbTest3.db', // 数据库文件名
          securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别
          encrypt: false, // 可选参数,指定数据库是否加密,默认不加密
        };
        let rdbStore = await relationalStore.getRdbStore(getContext(), STORE_CONFIG)
        let res = rdbStore.querySqlSync('SELECT NAME,AGE FROM EMPLOYEE')
        console.info(`querySqlSync data successfully! row count:${res?.rowCount}`);
      })

    }
    .height('100%')
    .width('100%')
  }
}

RdbTest.ets

import relationalStore from '@ohos.data.relationalStore';
const SQL_CREATE_TABLE = 'CREATE TABLE IF NOT EXISTS EMPLOYEE (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT NOT NULL,AGE INTEGER,SALARY INTEGER)';

export default class RdbTest {

  async batchInsert(context: Context, valueBucketArray: Array<relationalStore.ValuesBucket>) {
    const STORE_CONFIG: relationalStore.StoreConfig = {
      name: 'RdbTest3.db', // 数据库文件名
      securityLevel: relationalStore.SecurityLevel.S1, // 数据库安全级别
      encrypt: false, // 可选参数,指定数据库是否加密,默认不加密
    };
    let RdbStore = await relationalStore.getRdbStore(context, STORE_CONFIG)
    console.log("Succeeded in getting RdbStore.")
    await RdbStore.executeSql(SQL_CREATE_TABLE)
    console.log("建表成功")
    try {
      RdbStore.batchInsert("EMPLOYEE", valueBucketArray);
      console.info(`Insert data successfully!`);
    } catch (err) {
      console.error(`Insert datae failed! err code:${err.code}, err message:${err.message}`)
    }
  }
}
分享
微博
QQ
微信
回复
1天前
相关问题
taskPool @Concurrent报红
2707浏览 • 1回复 待解决
HarmonyOS 关系型数据库批量插入数据
376浏览 • 1回复 待解决
TaskPool里面是否可以使用EventHub
1788浏览 • 1回复 待解决
可以用家里智慧屏调试程序
9430浏览 • 2回复 已解决
HarmonyOS taskpool使用crash
1435浏览 • 2回复 待解决