1/* 2 * Copyright (c) 2023 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 16import ability from '@ohos.ability.ability'; 17import featureAbility from '@ohos.ability.featureAbility'; 18import { BusinessError } from '@ohos.base'; 19import ohos_data_ability from '@ohos.data.dataAbility'; 20import rdb from '@ohos.data.rdb'; 21import promptAction from '@ohos.promptAction'; 22import hilog from '@ohos.hilog'; 23 24const TAG: string = 'PageDataAbility'; 25const domain: number = 0xFF00; 26 27@Entry 28@Component 29struct PageDataAbility { 30 private valuesBucket_insert: rdb.ValuesBucket = { name: 'Rose', introduction: 'insert' }; 31 private valuesBucket_update: rdb.ValuesBucket = { name: 'Rose', introduction: 'update' }; 32 private crowd = new Array({ name: 'Rose', introduction: 'batchInsert_one' } as rdb.ValuesBucket, 33 { name: 'Rose', introduction: 'batchInsert_two' } as rdb.ValuesBucket); 34 private columnArray = new Array('id', 'name', 'introduction'); 35 private predicates = new ohos_data_ability.DataAbilityPredicates(); 36 private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility'; 37 private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri); 38 39 build() { 40 Column() { 41 Row() { 42 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 43 Text($r('app.string.dataAbility_button')) 44 .fontSize(24) 45 .fontWeight(FontWeight.Bold) 46 .textAlign(TextAlign.Start) 47 .margin({ top: 12, bottom: 11, right: 24, left: 24 }) 48 } 49 } 50 .width('100%') 51 .height(56) 52 .justifyContent(FlexAlign.Start) 53 .backgroundColor($r('app.color.backGrounding')) 54 55 56 List({ initialIndex: 0 }) { 57 ListItem() { 58 Text($r('app.string.insertData')) 59 .textAlign(TextAlign.Start) 60 .fontWeight(FontWeight.Medium) 61 .margin({ top: 19.5, bottom: 9.5 }) 62 .fontSize(14) 63 .width(176) 64 .height(22) 65 .fontColor($r('app.color.text_color')) 66 .opacity(0.6) 67 } 68 .height(48) 69 .width(176) 70 .margin({ top: 8, left: 24 }) 71 72 ListItemGroup() { 73 ListItem() { 74 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 75 Text($r('app.string.method_callback')) 76 .fontWeight(FontWeight.Medium) 77 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 78 .fontSize(16) 79 .width(232) 80 .height(22) 81 .fontColor($r('app.color.text_color')) 82 83 Image($r('app.media.ic_arrow')) 84 .width(24) 85 .height(24) 86 .margin({ top: 16, right: 6 }) 87 } 88 .onClick(() => { 89 // callback方式调用: 90 this.DAHelper.insert(this.uri, this.valuesBucket_insert, (error: BusinessError, data: number) => { 91 if (error && error.code !== 0) { 92 promptAction.showToast({ 93 message: $r('app.string.insert_failed_toast') 94 }); 95 } else { 96 promptAction.showToast({ 97 message: $r('app.string.insert_success_toast') 98 }); 99 } 100 hilog.info(domain, TAG, 'DAHelper insert result: ' + data + ', error: ' + JSON.stringify(error)); 101 } 102 ); 103 }) 104 } 105 .height(51) 106 .width('100%') 107 .margin({ top: 4 }) 108 109 ListItem() { 110 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 111 Text($r('app.string.method_promise')) 112 .fontWeight(FontWeight.Medium) 113 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 114 .fontSize(16) 115 .width(232) 116 .height(22) 117 .fontColor($r('app.color.text_color')) 118 119 Image($r('app.media.ic_arrow')) 120 .width(24) 121 .height(24) 122 .margin({ top: 16, right: 6 }) 123 } 124 .onClick(() => { 125 // promise方式调用(await需要在async方法中使用): 126 this.DAHelper.insert(this.uri, this.valuesBucket_insert).then((datainsert) => { 127 promptAction.showToast({ 128 message: $r('app.string.insert_success_toast') 129 }); 130 hilog.info(domain, TAG, 'DAHelper insert result: ' + datainsert); 131 }).catch((error: BusinessError) => { 132 promptAction.showToast({ 133 message: $r('app.string.insert_failed_toast') 134 }); 135 hilog.error(domain, TAG, `DAHelper insert failed. Cause: ${error.message}`); 136 }); 137 }) 138 } 139 .height(52) 140 .width('100%') 141 .margin({ bottom: 4 }) 142 143 } 144 .borderRadius(24) 145 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 146 .height(104) 147 .backgroundColor($r('app.color.start_window_background')) 148 .margin({ right: 12, left: 12 }) 149 150 ListItem() { 151 Text($r('app.string.delete_data')) 152 .textAlign(TextAlign.Start) 153 .fontWeight(FontWeight.Medium) 154 .margin({ top: 19.5, bottom: 9.5 }) 155 .fontSize(14) 156 .width(176) 157 .height(22) 158 .fontColor($r('app.color.text_color')) 159 .opacity(0.6) 160 } 161 .height(48) 162 .width(176) 163 .margin({ top: 8, left: 24 }) 164 165 ListItemGroup() { 166 ListItem() { 167 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 168 Text($r('app.string.method_callback')) 169 .fontWeight(FontWeight.Medium) 170 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 171 .fontSize(16) 172 .width(232) 173 .height(22) 174 .fontColor($r('app.color.text_color')) 175 176 Image($r('app.media.ic_arrow')) 177 .width(24) 178 .height(24) 179 .margin({ top: 16, right: 6 }) 180 } 181 .onClick(() => { 182 // callback方式调用: 183 this.DAHelper.delete(this.uri, this.predicates, (error, data) => { 184 if (error && error.code !== 0) { 185 promptAction.showToast({ 186 message: $r('app.string.delete_failed_toast') 187 }); 188 } else { 189 promptAction.showToast({ 190 message: $r('app.string.delete_success_toast') 191 }); 192 } 193 hilog.info(domain, TAG, 'DAHelper delete result: ' + data + ', error: ' + JSON.stringify(error)); 194 } 195 ); 196 }) 197 } 198 .height(51) 199 .width('100%') 200 .margin({ top: 4 }) 201 202 ListItem() { 203 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 204 Text($r('app.string.method_promise')) 205 .fontWeight(FontWeight.Medium) 206 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 207 .fontSize(16) 208 .width(232) 209 .height(22) 210 .fontColor($r('app.color.text_color')) 211 212 Image($r('app.media.ic_arrow')) 213 .width(24) 214 .height(24) 215 .margin({ top: 16, right: 6 }) 216 } 217 .onClick(() => { 218 // promise方式调用(await需要在async方法中使用): 219 this.DAHelper.delete(this.uri, this.predicates).then((datadelete) => { 220 promptAction.showToast({ 221 message: $r('app.string.delete_success_toast') 222 }); 223 hilog.info(domain, TAG, 'DAHelper delete result: ' + datadelete); 224 }).catch((error: BusinessError) => { 225 promptAction.showToast({ 226 message: $r('app.string.delete_failed_toast') 227 }); 228 hilog.error(domain, TAG, `DAHelper delete failed. Cause: ${error.message}`); 229 }); 230 }) 231 } 232 .height(52) 233 .width('100%') 234 .margin({ bottom: 4 }) 235 } 236 .borderRadius(24) 237 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 238 .height(104) 239 .backgroundColor($r('app.color.start_window_background')) 240 .margin({ right: 12, left: 12 }) 241 242 ListItem() { 243 Text($r('app.string.update_data')) 244 .textAlign(TextAlign.Start) 245 .fontWeight(FontWeight.Medium) 246 .margin({ top: 19.5, bottom: 9.5 }) 247 .fontSize(14) 248 .width(176) 249 .height(22) 250 .fontColor($r('app.color.text_color')) 251 .opacity(0.6) 252 } 253 .height(48) 254 .width(176) 255 .margin({ top: 8, left: 24 }) 256 257 ListItemGroup() { 258 ListItem() { 259 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 260 Text($r('app.string.method_callback')) 261 .fontWeight(FontWeight.Medium) 262 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 263 .fontSize(16) 264 .width(232) 265 .height(22) 266 .fontColor($r('app.color.text_color')) 267 268 Image($r('app.media.ic_arrow')) 269 .width(24) 270 .height(24) 271 .margin({ top: 16, right: 6 }) 272 } 273 .onClick(() => { 274 // callback方式调用: 275 this.predicates.equalTo('name', 'Rose'); 276 this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates, (error, data) => { 277 if (error && error.code !== 0) { 278 promptAction.showToast({ 279 message: $r('app.string.update_failed_toast') 280 }); 281 } else { 282 promptAction.showToast({ 283 message: $r('app.string.update_success_toast') 284 }); 285 } 286 hilog.info(domain, TAG, 'DAHelper update result: ' + data + ', error: ' + JSON.stringify(error)); 287 } 288 ); 289 }) 290 } 291 .height(51) 292 .width('100%') 293 .margin({ top: 4 }) 294 295 ListItem() { 296 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 297 Text($r('app.string.method_promise')) 298 .fontWeight(FontWeight.Medium) 299 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 300 .fontSize(16) 301 .width(232) 302 .height(22) 303 .fontColor($r('app.color.text_color')) 304 Image($r('app.media.ic_arrow')) 305 .width(24) 306 .height(24) 307 .margin({ top: 16, right: 6 }) 308 } 309 .onClick(() => { 310 // promise方式调用(await需要在async方法中使用): 311 this.predicates.equalTo('name', 'Rose'); 312 this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates).then((dataupdate) => { 313 promptAction.showToast({ 314 message: $r('app.string.update_success_toast') 315 }); 316 hilog.info(domain, TAG, 'DAHelper update result: ' + dataupdate); 317 }).catch((error: BusinessError) => { 318 promptAction.showToast({ 319 message: $r('app.string.update_failed_toast') 320 }); 321 hilog.error(domain, TAG, `DAHelper update failed. Cause: ${error.message}`); 322 }); 323 }) 324 } 325 .height(52) 326 .width('100%') 327 .margin({ bottom: 4 }) 328 } 329 .borderRadius(24) 330 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 331 .height(104) 332 .backgroundColor($r('app.color.start_window_background')) 333 .margin({ right: 12, left: 12 }) 334 335 ListItem() { 336 Text($r('app.string.query_data')) 337 .textAlign(TextAlign.Start) 338 .fontWeight(FontWeight.Medium) 339 .margin({ top: 19.5, bottom: 9.5 }) 340 .fontSize(14) 341 .width(176) 342 .height(22) 343 .fontColor($r('app.color.text_color')) 344 .opacity(0.6) 345 } 346 .height(48) 347 .width(176) 348 .margin({ top: 8, left: 24 }) 349 350 ListItemGroup() { 351 ListItem() { 352 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 353 Text($r('app.string.method_callback')) 354 .fontWeight(FontWeight.Medium) 355 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 356 .fontSize(16) 357 .width(232) 358 .height(22) 359 .fontColor($r('app.color.text_color')) 360 361 Image($r('app.media.ic_arrow')) 362 .width(24) 363 .height(24) 364 .margin({ top: 16, right: 6 }) 365 } 366 .onClick(() => { 367 // callback方式调用: 368 this.predicates.equalTo('name', 'Rose'); 369 this.DAHelper.query(this.uri, this.columnArray, this.predicates, (error, data) => { 370 if (error && error.code !== 0) { 371 promptAction.showToast({ 372 message: $r('app.string.query_failed_toast') 373 }); 374 hilog.error(domain, TAG, `DAHelper query failed. Cause: ${error.message}`); 375 } else { 376 promptAction.showToast({ 377 message: $r('app.string.query_success_toast') 378 }); 379 } 380 // ResultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 381 while (data.goToNextRow()) { 382 const id = data.getLong(data.getColumnIndex('id')); 383 const name = data.getString(data.getColumnIndex('name')); 384 const introduction = data.getString(data.getColumnIndex('introduction')); 385 hilog.info(domain, TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`); 386 } 387 // 释放数据集的内存 388 data.close(); 389 } 390 ); 391 }) 392 } 393 .height(51) 394 .width('100%') 395 .margin({ top: 4 }) 396 397 ListItem() { 398 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 399 Text($r('app.string.method_promise')) 400 .fontWeight(FontWeight.Medium) 401 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 402 .fontSize(16) 403 .width(232) 404 .height(22) 405 .fontColor($r('app.color.text_color')) 406 407 Image($r('app.media.ic_arrow')) 408 .width(24) 409 .height(24) 410 .margin({ top: 16, right: 6 }) 411 } 412 .onClick(() => { 413 // promise方式调用(await需要在async方法中使用): 414 this.predicates.equalTo('name', 'Rose'); 415 this.DAHelper.query(this.uri, this.columnArray, this.predicates).then((dataquery) => { 416 promptAction.showToast({ 417 message: $r('app.string.query_success_toast') 418 }); 419 // ResultSet是一个数据集合的游标,默认指向第-1个记录,有效的数据从0开始。 420 while (dataquery.goToNextRow()) { 421 const id = dataquery.getLong(dataquery.getColumnIndex('id')); 422 const name = dataquery.getString(dataquery.getColumnIndex('name')); 423 const introduction = dataquery.getString(dataquery.getColumnIndex('introduction')); 424 hilog.info(domain, TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`); 425 } 426 // 释放数据集的内存 427 dataquery.close(); 428 }).catch((error: BusinessError) => { 429 promptAction.showToast({ 430 message: $r('app.string.query_failed_toast') 431 }); 432 hilog.error(domain, TAG, `DAHelper query failed. Cause: ${error.message}`); 433 }); 434 }) 435 } 436 .height(52) 437 .width('100%') 438 .margin({ bottom: 4 }) 439 } 440 .borderRadius(24) 441 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 442 .height(104) 443 .backgroundColor($r('app.color.start_window_background')) 444 .margin({ right: 12, left: 12 }) 445 446 ListItem() { 447 Text($r('app.string.batchInsert_data')) 448 .textAlign(TextAlign.Start) 449 .fontWeight(FontWeight.Medium) 450 .margin({ top: 19.5, bottom: 9.5 }) 451 .fontSize(14) 452 .width(176) 453 .height(22) 454 .fontColor($r('app.color.text_color')) 455 .opacity(0.6) 456 } 457 .height(48) 458 .width(176) 459 .margin({ top: 8, left: 24 }) 460 461 ListItemGroup() { 462 ListItem() { 463 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 464 Text($r('app.string.method_callback')) 465 .fontWeight(FontWeight.Medium) 466 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 467 .fontSize(16) 468 .width(232) 469 .height(22) 470 .fontColor($r('app.color.text_color')) 471 472 Image($r('app.media.ic_arrow')) 473 .width(24) 474 .height(24) 475 .margin({ top: 16, right: 6 }) 476 } 477 .onClick(() => { 478 // callback方式调用: 479 this.DAHelper.batchInsert(this.uri, this.crowd, (error, data) => { 480 if (error && error.code !== 0) { 481 promptAction.showToast({ 482 message: $r('app.string.batchInsert_failed_toast') 483 }); 484 } else { 485 promptAction.showToast({ 486 message: $r('app.string.batchInsert_success_toast') 487 }); 488 } 489 hilog.info(domain, TAG, 'DAHelper batchInsert result: ' + data + ', error: ' + JSON.stringify(error)); 490 } 491 ); 492 }) 493 } 494 .height(51) 495 .width('100%') 496 .margin({ top: 4 }) 497 498 ListItem() { 499 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 500 Text($r('app.string.method_promise')) 501 .fontWeight(FontWeight.Medium) 502 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 503 .fontSize(16) 504 .width(232) 505 .height(22) 506 .fontColor($r('app.color.text_color')) 507 508 Image($r('app.media.ic_arrow')) 509 .width(24) 510 .height(24) 511 .margin({ top: 16, right: 6 }) 512 } 513 .onClick(() => { 514 // promise方式调用(await需要在async方法中使用): 515 this.DAHelper.batchInsert(this.uri, this.crowd).then((databatchInsert) => { 516 promptAction.showToast({ 517 message: $r('app.string.batchInsert_success_toast') 518 }); 519 hilog.info(domain, TAG, 'DAHelper batchInsert result: ' + databatchInsert); 520 }).catch((error: BusinessError) => { 521 promptAction.showToast({ 522 message: $r('app.string.batchInsert_failed_toast') 523 }); 524 hilog.error(domain, TAG, `DAHelper batchInsert failed. Cause: ${error.message}`); 525 }); 526 }) 527 } 528 .height(52) 529 .width('100%') 530 .margin({ bottom: 4 }) 531 } 532 .borderRadius(24) 533 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 534 .height(104) 535 .backgroundColor($r('app.color.start_window_background')) 536 .margin({ right: 12, left: 12 }) 537 538 ListItem() { 539 Text($r('app.string.executeBatch_data')) 540 .textAlign(TextAlign.Start) 541 .fontWeight(FontWeight.Medium) 542 .margin({ top: 19.5, bottom: 9.5 }) 543 .fontSize(14) 544 .width(176) 545 .height(22) 546 .fontColor($r('app.color.text_color')) 547 .opacity(0.6) 548 } 549 .height(48) 550 .width(176) 551 .margin({ top: 8, left: 24 }) 552 553 ListItemGroup() { 554 ListItem() { 555 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 556 Text($r('app.string.method_callback')) 557 .fontWeight(FontWeight.Medium) 558 .margin({ top: 13, bottom: 12.5, left: 12, right: 92 }) 559 .fontSize(16) 560 .width(232) 561 .height(22) 562 .fontColor($r('app.color.text_color')) 563 564 Image($r('app.media.ic_arrow')) 565 .width(24) 566 .height(24) 567 .margin({ top: 16, right: 6 }) 568 } 569 .onClick(() => { 570 // callback方式调用: 571 let operations: Array<ability.DataAbilityOperation> = [{ 572 uri: this.uri, 573 type: featureAbility.DataAbilityOperationType.TYPE_INSERT, 574 valuesBucket: { name: 'Rose', introduction: 'executeBatch' }, 575 predicates: this.predicates, 576 expectedCount: 0, 577 predicatesBackReferences: undefined, 578 interrupted: true, 579 }]; 580 this.DAHelper.executeBatch(this.uri, operations, (error, data) => { 581 if (error && error.code !== 0) { 582 promptAction.showToast({ 583 message: $r('app.string.executeBatch_failed_toast') 584 }); 585 } else { 586 promptAction.showToast({ 587 message: $r('app.string.executeBatch_success_toast') 588 }); 589 } 590 hilog.info(domain, TAG, `DAHelper executeBatch, result: ` + JSON.stringify(data) + ', error: ' + JSON.stringify(error)); 591 }); 592 }) 593 } 594 .height(51) 595 .width('100%') 596 .margin({ top: 4 }) 597 598 ListItem() { 599 Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) { 600 Text($r('app.string.method_promise')) 601 .fontWeight(FontWeight.Medium) 602 .margin({ top: 13, bottom: 13, left: 12, right: 92 }) 603 .fontSize(16) 604 .width(232) 605 .height(22) 606 .fontColor($r('app.color.text_color')) 607 608 Image($r('app.media.ic_arrow')) 609 .width(24) 610 .height(24) 611 .margin({ top: 16, right: 6 }) 612 } 613 .onClick(() => { 614 // promise方式调用(await需要在async方法中使用): 615 let operations: Array<ability.DataAbilityOperation> = [{ 616 uri: this.uri, 617 type: featureAbility.DataAbilityOperationType.TYPE_INSERT, 618 valuesBucket: { name: 'Rose', introduction: 'executeBatch' }, 619 predicates: this.predicates, 620 expectedCount: 0, 621 predicatesBackReferences: undefined, 622 interrupted: true, 623 }]; 624 this.DAHelper.executeBatch(this.uri, operations).then((dataquery) => { 625 promptAction.showToast({ 626 message: $r('app.string.executeBatch_success_toast') 627 }); 628 hilog.info(domain, TAG, 'DAHelper executeBatch result: ' + JSON.stringify(dataquery)); 629 }).catch((error: BusinessError) => { 630 promptAction.showToast({ 631 message: $r('app.string.executeBatch_failed_toast') 632 }); 633 hilog.error(domain, TAG, `DAHelper executeBatch failed. Cause: ${error.message}`); 634 }); 635 }) 636 } 637 .height(52) 638 .width('100%') 639 .margin({ bottom: 4 }) 640 } 641 .borderRadius(24) 642 .divider({ strokeWidth: 0.5, color: ($r('app.color.backGrounding')), startMargin: 12, endMargin: 12 }) 643 .height(104) 644 .backgroundColor($r('app.color.start_window_background')) 645 .margin({ right: 12, left: 12 }) 646 } 647 .height('92%') 648 .backgroundColor($r('app.color.backGrounding')) 649 } 650 .width('100%') 651 .margin({ top: 8 }) 652 .backgroundColor($r('app.color.backGrounding')) 653 } 654}