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

HarmonyOS
2024-12-25 11:08:50
浏览
收藏 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%')
  }
}
  • 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.

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}`)
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
分享
微博
QQ
微信
回复
2024-12-25 13:35:40
相关问题
taskPool @Concurrent报红
3648浏览 • 1回复 待解决
HarmonyOS 关系型数据库批量插入数据
1330浏览 • 1回复 待解决
TaskPool里面是否可以使用EventHub
2595浏览 • 1回复 待解决
HarmonyOS @Concurrent
533浏览 • 1回复 待解决