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 operate_child_thread_data] 17import { relationalStore, ValuesBucket } from '@kit.ArkData'; 18import { taskpool } from '@kit.ArkTS'; 19 20@Concurrent 21async function create(context: Context) { 22 const CONFIG: relationalStore.StoreConfig = { 23 name: 'Store.db', 24 securityLevel: relationalStore.SecurityLevel.S1, 25 }; 26 27 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 28 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 29 console.info(`Create Store.db successfully!`); 30 31 // 创建表 32 const CREATE_TABLE_SQL = 'CREATE TABLE IF NOT EXISTS test (' + 33 'id INTEGER PRIMARY KEY AUTOINCREMENT, ' + 34 'name TEXT NOT NULL, ' + 35 'age INTEGER, ' + 36 'salary REAL, ' + 37 'blobType BLOB)'; 38 await store.executeSql(CREATE_TABLE_SQL); 39 console.info(`Create table test successfully!`); 40} 41 42@Concurrent 43async function insert(context: Context, valueBucketArray: Array<relationalStore.ValuesBucket>) { 44 const CONFIG: relationalStore.StoreConfig = { 45 name: 'Store.db', 46 securityLevel: relationalStore.SecurityLevel.S1, 47 }; 48 49 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 50 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 51 console.info(`Create Store.db successfully!`); 52 53 // 数据插入 54 await store.batchInsert('test', valueBucketArray as Object as Array<relationalStore.ValuesBucket>); 55} 56 57@Concurrent 58async function query(context: Context): Promise<Array<relationalStore.ValuesBucket>> { 59 const CONFIG: relationalStore.StoreConfig = { 60 name: 'Store.db', 61 securityLevel: relationalStore.SecurityLevel.S1, 62 }; 63 64 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 65 let store: relationalStore.RdbStore = await relationalStore.getRdbStore(context, CONFIG); 66 console.info(`Create Store.db successfully!`); 67 68 // 获取结果集 69 let predicates: relationalStore.RdbPredicates = new relationalStore.RdbPredicates('test'); 70 let resultSet = await store.query(predicates); // 查询所有数据 71 console.info(`Query data successfully! row count:${resultSet.rowCount}`); 72 let index = 0; 73 let result = new Array<relationalStore.ValuesBucket>(resultSet.rowCount) 74 resultSet.goToFirstRow() 75 do { 76 result[index++] = resultSet.getRow(); 77 } while (resultSet.goToNextRow()); 78 resultSet.close(); 79 return result; 80} 81 82@Concurrent 83async function clear(context: Context) { 84 const CONFIG: relationalStore.StoreConfig = { 85 name: 'Store.db', 86 securityLevel: relationalStore.SecurityLevel.S1, 87 }; 88 89 // 默认数据库文件路径为 context.databaseDir + rdb + StoreConfig.name 90 await relationalStore.deleteRdbStore(context, CONFIG); 91 console.info(`Delete Store.db successfully!`); 92} 93 94@Entry 95@Component 96struct Index { 97 @State message: string = 'Hello World'; 98 99 build() { 100 RelativeContainer() { 101 Text(this.message) 102 .id('HelloWorld') 103 .fontSize(50) 104 .fontWeight(FontWeight.Bold) 105 .alignRules({ 106 center: { anchor: '__container__', align: VerticalAlign.Center }, 107 middle: { anchor: '__container__', align: HorizontalAlign.Center } 108 }) 109 .onClick(async () => { 110 let context = getContext(this); 111 112 // 数据准备 113 const count = 5 114 let valueBucketArray = new Array<relationalStore.ValuesBucket>(count); 115 for (let i = 0; i < count; i++) { 116 let v: relationalStore.ValuesBucket = { 117 id: i, 118 name: 'zhangsan' + i, 119 age: 20, 120 salary: 5000 + 50 * i 121 }; 122 valueBucketArray[i] = v; 123 } 124 await taskpool.execute(create, context); 125 await taskpool.execute(insert, context, valueBucketArray); 126 let index = 0; 127 let ret = await taskpool.execute(query, context) as Array<relationalStore.ValuesBucket>; 128 for (let v of ret) { 129 console.info(`Row[${index}].id = ${v.id}`) 130 console.info(`Row[${index}].name = ${v.name}`) 131 console.info(`Row[${index}].age = ${v.age}`) 132 console.info(`Row[${index}].salary = ${v.salary}`) 133 index++ 134 }; 135 await taskpool.execute(clear, context); 136 this.message = 'success'; 137 }) 138 } 139 .height('100%') 140 .width('100%') 141 } 142} 143// [End operate_child_thread_data] 144