1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16// [Start taskpool_frequently_operate_database] 17import { relationalStore, ValuesBucket } from '@kit.ArkData'; 18import { collections, taskpool } from '@kit.ArkTS'; 19import { IValueBucket, SharedValuesBucket } from './SharedValuesBucket'; 20 21@Concurrent 22async function create(context: Context) { 23 const CONFIG: relationalStore.StoreConfig = { 24 name: 'Store.db', 25 securityLevel: relationalStore.SecurityLevel.S1, 26 }; 27 28 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 29 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 30 console.info(`Create Store.db successfully!`); 31 32 // 创建表 33 const CREATE_TABLE_SQL = 'CREATE TABLE IF NOT EXISTS test (' + 34 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 35 'name TEXT NOT NULL, ' + 36 'age INTEGER, ' + 37 'salary REAL, ' + 38 'blobType BLOB)'; 39 await store.executeSql(CREATE_TABLE_SQL); 40 console.info(`Create table test successfully!`); 41} 42 43@Concurrent 44async function insert(context: Context, valueBucketArray: Array<relationalStore.ValuesBucket>) { 45 const CONFIG: relationalStore.StoreConfig = { 46 name: 'Store.db', 47 securityLevel: relationalStore.SecurityLevel.S1, 48 }; 49 50 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 51 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 52 console.info(`Create Store.db successfully!`); 53 54 // 数据插入 55 await store.batchInsert('test', valueBucketArray as Object as Array<relationalStore.ValuesBucket>); 56} 57 58@Concurrent 59async function query(context: Context): Promise<Array<relationalStore.ValuesBucket>> { 60 const CONFIG: relationalStore.StoreConfig = { 61 name: 'Store.db', 62 securityLevel: relationalStore.SecurityLevel.S1, 63 }; 64 65 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 66 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 67 console.info(`Create Store.db successfully!`); 68 69 // 获取结果集 70 let predicates: relationalStore.RdbPredicates = new relationalStore.RdbPredicates('test'); 71 let resultSet = await store.query(predicates); // 查询所有数据 72 console.info(`Query data successfully! row count:${resultSet.rowCount}`); 73 let index = 0; 74 let result = new Array<relationalStore.ValuesBucket>(resultSet.rowCount); 75 resultSet.goToFirstRow(); 76 do { 77 result[index++] = resultSet.getRow(); 78 } while (resultSet.goToNextRow()); 79 resultSet.close(); 80 return result; 81} 82 83@Concurrent 84async function clear(context: Context) { 85 const CONFIG: relationalStore.StoreConfig = { 86 name: 'Store.db', 87 securityLevel: relationalStore.SecurityLevel.S1, 88 }; 89 90 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 91 await relationalStore.deleteRdbStore(context, CONFIG); 92 console.info(`Delete Store.db successfully!`); 93} 94 95@Entry 96@Component 97struct Index { 98 @State message: string = 'Hello World'; 99 100 build() { 101 RelativeContainer() { 102 Text(this.message) 103 .id('HelloWorld') 104 .fontSize(50) 105 .fontWeight(FontWeight.Bold) 106 .alignRules({ 107 center: { anchor: '__container__', align: VerticalAlign.Center }, 108 middle: { anchor: '__container__', align: HorizontalAlign.Center } 109 }) 110 .onClick(async () => { 111 let context = getContext(this); 112 113 // 数据准备 114 const count = 5 115 let valueBucketArray = collections.Array.create<SharedValuesBucket | undefined>(count, undefined); 116 for (let i = 0; i < count; i++) { 117 let v: IValueBucket = { 118 id: i, 119 name: 'zhangsan' + i, 120 age: 20, 121 salary: 5000 + 50 * i 122 }; 123 valueBucketArray[i] = new SharedValuesBucket(v); 124 } 125 await taskpool.execute(create, context); 126 await taskpool.execute(insert, context, valueBucketArray); 127 let index = 0; 128 let ret: collections.Array<SharedValuesBucket> = 129 await taskpool.execute(query, context) as collections.Array<SharedValuesBucket>; 130 for (let v of ret.values()) { 131 console.info(`Row[${index}].id = ${v.id}`) 132 console.info(`Row[${index}].name = ${v.name}`) 133 console.info(`Row[${index}].age = ${v.age}`) 134 console.info(`Row[${index}].salary = ${v.salary}`) 135 index++ 136 }; 137 await taskpool.execute(clear, context); 138 this.message = 'success'; 139 }) 140 } 141 .height('100%') 142 .width('100%') 143 } 144} 145// [End taskpool_frequently_operate_database] 146