0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復
登錄后你可以
  • 下載海量資料
  • 學習在線課程
  • 觀看技術視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領取20積分哦,立即完善>

3天內(nèi)不再提示

鴻蒙開發(fā)組件:啟動與訪問DataAbility

jf_46214456 ? 來源:jf_46214456 ? 作者:jf_46214456 ? 2024-06-20 15:03 ? 次閱讀

啟動DataAbility

啟動DataAbility會獲取一個工具接口類對象(DataAbilityHelper)。啟動DataAbility的示例代碼如下:

import featureAbility from '@ohos.ability.featureAbility';
import ability from '@ohos.ability.ability';

let uri: string = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(uri);

訪問DataAbility

訪問DataAbility需導入基礎依賴包,以及獲取與DataAbility子模塊通信的URI字符串。

其中,基礎依賴包包括: 開發(fā)前請熟悉鴻蒙開發(fā)指導文檔 :[gitee.com/li-shizhen-skin/harmony-os/blob/master/README.md]

搜狗高速瀏覽器截圖20240326151547.png

  • @ohos.ability.featureAbility
  • @ohos.data.dataAbility

訪問DataAbility的示例代碼如下:

  1. 創(chuàng)建工具接口類對象。

    import featureAbility from '@ohos.ability.featureAbility';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import ability from '@ohos.ability.ability';
    // 作為參數(shù)傳遞的URI,與config中定義的URI的區(qū)別是多了一個"/",有三個"/"
    let uri: string = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
    let DAHelper: ability.DataAbilityHelper = featureAbility.acquireDataAbilityHelper(uri);
    
  2. 構(gòu)建數(shù)據(jù)庫相關的RDB數(shù)據(jù)。

    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    let valuesBucket_insert: rdb.ValuesBucket = { name: 'Rose', introduction: 'insert' };
    let valuesBucket_update: rdb.ValuesBucket = { name: 'Rose', introduction: 'update' };
    let crowd = new Array({ name: 'Rose', introduction: 'batchInsert_one' } as rdb.ValuesBucket,
      { name: 'Rose', introduction: 'batchInsert_two' } as rdb.ValuesBucket);
    let columnArray = new Array('id', 'name', 'introduction');
    let predicates = new ohos_data_ability.DataAbilityPredicates();
    

    注:關于DataAbilityPredicates的詳細內(nèi)容,請參考[DataAbility謂詞]。

  3. 調(diào)用insert方法向指定的DataAbility子模塊插入數(shù)據(jù)。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    
    // callback方式調(diào)用:
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private valuesBucket_insert: rdb.ValuesBucket = { name: 'Rose', introduction: 'insert' };
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  this.DAHelper.insert(this.uri, this.valuesBucket_insert, (error: BusinessError, data: number) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.insert_failed_toast')
                      });
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.insert_success_toast')
                      });
                    }
                    hilog.info(domain, TAG, 'DAHelper insert result: ' + data + ', error: ' + JSON.stringify(error));
                  }
                  );
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private valuesBucket_insert: rdb.ValuesBucket = { name: 'Rose', introduction: 'insert' };
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  this.DAHelper.insert(this.uri, this.valuesBucket_insert).then((datainsert) = > {
                    promptAction.showToast({
                      message: $r('app.string.insert_success_toast')
                    });
                    hilog.info(domain, TAG, 'DAHelper insert result: ' + datainsert);
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.insert_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper insert failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
  4. 調(diào)用delete方法刪除DataAbility子模塊中指定的數(shù)據(jù)。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  this.DAHelper.delete(this.uri, this.predicates, (error, data) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.delete_failed_toast')
                      });
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.delete_success_toast')
                      });
                    }
                    hilog.info(domain, TAG, 'DAHelper delete result: ' + data + ', error: ' + JSON.stringify(error));
                  }
                  );
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  this.DAHelper.delete(this.uri, this.predicates).then((datadelete) = > {
                    promptAction.showToast({
                      message: $r('app.string.delete_success_toast')
                    });
                    hilog.info(domain, TAG, 'DAHelper delete result: ' + datadelete);
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.delete_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper delete failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
  5. 調(diào)用update方法更新指定DataAbility子模塊中的數(shù)據(jù)。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private valuesBucket_update: rdb.ValuesBucket = { name: 'Rose', introduction: 'update' };
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  this.predicates.equalTo('name', 'Rose');
                  this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates, (error, data) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.update_failed_toast')
                      });
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.update_success_toast')
                      });
                    }
                    hilog.info(domain, TAG, 'DAHelper update result: ' + data + ', error: ' + JSON.stringify(error));
                  }
                  );
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private valuesBucket_update: rdb.ValuesBucket = { name: 'Rose', introduction: 'update' };
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  this.predicates.equalTo('name', 'Rose');
                  this.DAHelper.update(this.uri, this.valuesBucket_update, this.predicates).then((dataupdate) = > {
                    promptAction.showToast({
                      message: $r('app.string.update_success_toast')
                    });
                    hilog.info(domain, TAG, 'DAHelper update result: ' + dataupdate);
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.update_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper update failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
  6. 調(diào)用query方法在指定的DataAbility子模塊中查找數(shù)據(jù)。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private columnArray = new Array('id', 'name', 'introduction');
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  this.predicates.equalTo('name', 'Rose');
                  this.DAHelper.query(this.uri, this.columnArray, this.predicates, (error, data) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.query_failed_toast')
                      });
                      hilog.error(domain, TAG, `DAHelper query failed. Cause: ${error.message}`);
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.query_success_toast')
                      });
                    }
                    // ResultSet是一個數(shù)據(jù)集合的游標,默認指向第-1個記錄,有效的數(shù)據(jù)從0開始。
                    while (data.goToNextRow()) {
                      const id = data.getLong(data.getColumnIndex('id'));
                      const name = data.getString(data.getColumnIndex('name'));
                      const introduction = data.getString(data.getColumnIndex('introduction'));
                      hilog.info(domain, TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`);
                    }
                    // 釋放數(shù)據(jù)集的內(nèi)存
                    data.close();
                  }
                  );
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private columnArray = new Array('id', 'name', 'introduction');
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  this.predicates.equalTo('name', 'Rose');
                  this.DAHelper.query(this.uri, this.columnArray, this.predicates).then((dataquery) = > {
                    promptAction.showToast({
                      message: $r('app.string.query_success_toast')
                    });
                    // ResultSet是一個數(shù)據(jù)集合的游標,默認指向第-1個記錄,有效的數(shù)據(jù)從0開始。
                    while (dataquery.goToNextRow()) {
                      const id = dataquery.getLong(dataquery.getColumnIndex('id'));
                      const name = dataquery.getString(dataquery.getColumnIndex('name'));
                      const introduction = dataquery.getString(dataquery.getColumnIndex('introduction'));
                      hilog.info(domain, TAG, `DAHelper query result:id = [${id}], name = [${name}], introduction = [${introduction}]`);
                    }
                    // 釋放數(shù)據(jù)集的內(nèi)存
                    dataquery.close();
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.query_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper query failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
  7. 調(diào)用batchInsert方法向指定的DataAbility子模塊批量插入數(shù)據(jù)。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private crowd = new Array({ name: 'Rose', introduction: 'batchInsert_one' } as rdb.ValuesBucket,
        { name: 'Rose', introduction: 'batchInsert_two' } as rdb.ValuesBucket);
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  this.DAHelper.batchInsert(this.uri, this.crowd, (error, data) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.batchInsert_failed_toast')
                      });
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.batchInsert_success_toast')
                      });
                    }
                    hilog.info(domain, TAG, 'DAHelper batchInsert result: ' + data + ', error: ' + JSON.stringify(error));
                  }
                  );
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private crowd = new Array({ name: 'Rose', introduction: 'batchInsert_one' } as rdb.ValuesBucket,
        { name: 'Rose', introduction: 'batchInsert_two' } as rdb.ValuesBucket);
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  this.DAHelper.batchInsert(this.uri, this.crowd).then((databatchInsert) = > {
                    promptAction.showToast({
                      message: $r('app.string.batchInsert_success_toast')
                    });
                    hilog.info(domain, TAG, 'DAHelper batchInsert result: ' + databatchInsert);
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.batchInsert_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper batchInsert failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    
  8. 調(diào)用executeBatch方法向指定的DataAbility子模塊進行數(shù)據(jù)的批量處理。

    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // callback方式調(diào)用:
                  let operations: Array< ability.DataAbilityOperation > = [{
                    uri: this.uri,
                    type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
                    valuesBucket: { name: 'Rose', introduction: 'executeBatch' },
                    predicates: this.predicates,
                    expectedCount: 0,
                    predicatesBackReferences: undefined,
                    interrupted: true,
                  }];
                  this.DAHelper.executeBatch(this.uri, operations, (error, data) = > {
                    if (error && error.code !== 0) {
                      promptAction.showToast({
                        message: $r('app.string.executeBatch_failed_toast')
                      });
                    } else {
                      promptAction.showToast({
                        message: $r('app.string.executeBatch_success_toast')
                      });
                    }
                    hilog.info(domain, TAG, `DAHelper executeBatch, result: ` + JSON.stringify(data) + ', error: ' + JSON.stringify(error));
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
       }
     }
    
    import ability from '@ohos.ability.ability';
    import featureAbility from '@ohos.ability.featureAbility';
    import { BusinessError } from '@ohos.base';
    import ohos_data_ability from '@ohos.data.dataAbility';
    import rdb from '@ohos.data.rdb';
    import promptAction from '@ohos.promptAction';
    import hilog from '@ohos.hilog';
    
    const TAG: string = 'PageDataAbility';
    const domain: number = 0xFF00;
    
    @Entry
    @Component
    struct PageDataAbility {
      private predicates = new ohos_data_ability.DataAbilityPredicates();
      private uri = 'dataability:///com.samples.famodelabilitydevelop.DataAbility';
      private DAHelper = featureAbility.acquireDataAbilityHelper(this.uri);
    
      build() {
        Column() {
          // ...
          List({ initialIndex: 0 }) {
            // ...
            ListItemGroup() {
              ListItem() {
                Flex({ justifyContent: FlexAlign.SpaceBetween, alignContent: FlexAlign.Center }) {
                  // ...
                }
                .onClick(() = > {
                  // promise方式調(diào)用(await需要在async方法中使用):
                  let operations: Array< ability.DataAbilityOperation > = [{
                    uri: this.uri,
                    type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
                    valuesBucket: { name: 'Rose', introduction: 'executeBatch' },
                    predicates: this.predicates,
                    expectedCount: 0,
                    predicatesBackReferences: undefined,
                    interrupted: true,
                  }];
                  this.DAHelper.executeBatch(this.uri, operations).then((dataquery) = > {
                    promptAction.showToast({
                      message: $r('app.string.executeBatch_success_toast')
                    });
                    hilog.info(domain, TAG, 'DAHelper executeBatch result: ' + JSON.stringify(dataquery));
                  }).catch((error: BusinessError) = > {
                    promptAction.showToast({
                      message: $r('app.string.executeBatch_failed_toast')
                    });
                    hilog.error(domain, TAG, `DAHelper executeBatch failed. Cause: ${error.message}`);
                  });
                })
              }
              // ...
            }
            // ...
          }
          // ...
        }
        // ...
      }
    }
    

審核編輯 黃宇

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學習之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 組件
    +關注

    關注

    1

    文章

    512

    瀏覽量

    17845
  • 鴻蒙
    +關注

    關注

    57

    文章

    2362

    瀏覽量

    42884
收藏 人收藏

    評論

    相關推薦

    鴻蒙開發(fā)Ability Kit程序框架服務:DataAbility切換 組件切換

    FA模型中的DataAbility對應Stage模型中的DataShareExtensionAbility。
    的頭像 發(fā)表于 06-28 14:08 ?267次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>Ability Kit程序框架服務:<b class='flag-5'>DataAbility</b>切換 <b class='flag-5'>組件</b>切換

    鴻蒙開發(fā)Ability Kit程序框架服務:FA模型與Stage模型應用組件互通綜述

    FA模型與Stage模型是兩套不同的應用模型,他們擁有各自的組件。FA模型提供三種應用組件,分別是PageAbility、ServiceAbility和DataAbility。Stage模型提供了兩種應用
    的頭像 發(fā)表于 06-24 16:43 ?538次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>Ability Kit程序框架服務:FA模型與Stage模型應用<b class='flag-5'>組件</b>互通綜述

    鴻蒙開發(fā)組件DataAbility權(quán)限控制

    DataAbility提供數(shù)據(jù)服務,并不是所有的Ability都有權(quán)限讀寫它,DataAbility有一套權(quán)限控制機制來保證數(shù)據(jù)安全。分為靜態(tài)權(quán)限控制和動態(tài)權(quán)限控制兩部分。
    的頭像 發(fā)表于 06-21 10:30 ?350次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>組件</b>:<b class='flag-5'>DataAbility</b>權(quán)限控制

    鴻蒙開發(fā)組件DataAbility的生命周期

    應用開發(fā)者可以根據(jù)業(yè)務場景實現(xiàn)data.js/data.ets中的生命周期相關接口。DataAbility生命周期接口說明見下表。
    的頭像 發(fā)表于 06-20 09:39 ?449次閱讀

    鴻蒙開發(fā)組件:創(chuàng)建DataAbility

    實現(xiàn)DataAbility中Insert、Query、Update、Delete接口的業(yè)務內(nèi)容。保證能夠滿足數(shù)據(jù)庫存儲業(yè)務的基本需求。BatchInsert與ExecuteBatch接口已經(jīng)在系統(tǒng)中實現(xiàn)遍歷邏輯,依賴Insert、Query、Update、Delete接口邏輯,來實現(xiàn)數(shù)據(jù)的批量處理。
    的頭像 發(fā)表于 06-19 21:41 ?294次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b><b class='flag-5'>組件</b>:創(chuàng)建<b class='flag-5'>DataAbility</b>

    鴻蒙開發(fā)接口數(shù)據(jù)管理:【@ohos.data.dataAbility (DataAbility謂詞)】

    DataAbility 謂詞用于構(gòu)造關系型數(shù)據(jù)庫的謂詞,提供用于實現(xiàn)不同查詢方法的謂詞。
    的頭像 發(fā)表于 06-06 17:20 ?855次閱讀
    <b class='flag-5'>鴻蒙</b><b class='flag-5'>開發(fā)</b>接口數(shù)據(jù)管理:【@ohos.data.<b class='flag-5'>dataAbility</b> (<b class='flag-5'>DataAbility</b>謂詞)】

    哪吒汽車APP啟動鴻蒙原生應用開發(fā)

    哪吒汽車正式簽約,啟動鴻蒙原生應用開發(fā);在5月17日,在上海市經(jīng)濟和信息化委員會指導的“千帆競發(fā)啟航 共筑鴻蒙生態(tài)——HDD上海站·鴻蒙原生
    的頭像 發(fā)表于 05-18 09:48 ?1178次閱讀

    鴻蒙實戰(zhàn)開發(fā)Camera組件:【相機】

    相機組件支持相機業(yè)務的開發(fā)開發(fā)者可以通過已開放的接口實現(xiàn)相機硬件的訪問、操作和新功能開發(fā),最常見的操作如:預覽、拍照和錄像等。
    的頭像 發(fā)表于 03-08 16:20 ?631次閱讀
    <b class='flag-5'>鴻蒙</b>實戰(zhàn)<b class='flag-5'>開發(fā)</b>Camera<b class='flag-5'>組件</b>:【相機】

    淘寶與華為合作將基于HarmonyOS NEXT啟動鴻蒙原生應用開發(fā)

    1月25日,淘寶與華為舉辦鴻蒙合作簽約儀式,宣布將基于HarmonyOS NEXT啟動鴻蒙原生應用開發(fā)。
    的頭像 發(fā)表于 01-26 16:14 ?1139次閱讀

    淘寶正式啟動華為鴻蒙原生應用開發(fā)

    華為與淘寶近日宣布,淘寶已正式啟動鴻蒙原生應用開發(fā)。這一合作標志著鴻蒙生態(tài)在電商領域的布局進一步擴大,將為用戶帶來全新的全場景購物體驗。
    的頭像 發(fā)表于 01-26 15:59 ?799次閱讀

    多家頭部企業(yè)宣布「啟動鴻蒙原生應用開發(fā)」,你看好鴻蒙系統(tǒng)走向「獨立」嗎?

    鴻蒙生態(tài)迎「關鍵一戰(zhàn)」,多家頭部企業(yè)宣布「啟動鴻蒙原生應用開發(fā)」,你看好鴻蒙系統(tǒng)走向「獨立」嗎?
    的頭像 發(fā)表于 01-24 11:47 ?578次閱讀
    多家頭部企業(yè)宣布「<b class='flag-5'>啟動</b><b class='flag-5'>鴻蒙</b>原生應用<b class='flag-5'>開發(fā)</b>」,你看好<b class='flag-5'>鴻蒙</b>系統(tǒng)走向「獨立」嗎?

    鴻蒙ArkUI開發(fā)-Tabs組件的使用

    鴻蒙ArkUI開發(fā)-Tabs組件的使用
    的頭像 發(fā)表于 01-19 16:01 ?1937次閱讀
    <b class='flag-5'>鴻蒙</b>ArkUI<b class='flag-5'>開發(fā)</b>-Tabs<b class='flag-5'>組件</b>的使用

    京東正式啟動鴻蒙原生應用開發(fā)

    華為與京東于近日宣布達成一項重要合作,正式啟動鴻蒙原生應用開發(fā)。這一合作旨在利用HarmonyOS NEXT的獨特優(yōu)勢,為消費者提供更出色的購物體驗。
    的頭像 發(fā)表于 01-18 15:02 ?792次閱讀

    鴻蒙開發(fā)OpenHarmony組件復用案例

    ) } }, item => item) } } } 本文主要是對鴻蒙開發(fā)基礎當中的OpenHarmony技術組件復用示例, 更多鴻蒙開發(fā)
    發(fā)表于 01-15 17:37

    鴻蒙開發(fā)基礎-Web組件之cookie操作

    }) ... } ... 本文章主要是對鴻蒙開發(fā)當中ArkTS語言的基礎應用實戰(zhàn),Web組件里的cookie操作。更多的鴻蒙應用開發(fā)
    發(fā)表于 01-14 21:31