• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Accessing a DataAbility
2
3
4To access a DataAbility, import the basic dependency packages and obtain the URI string for communicating with the DataAbility.
5
6
7The basic dependency packages include:
8
9
10- @ohos.ability.featureAbility
11
12- @ohos.data.dataAbility
13
14- @ohos.data.rdb
15
16
17The sample code for accessing a DataAbility is as follows:
18
19
201. Create a **DataAbilityHelper** instance.
21
22   ```ts
23   // Different from the URI defined in the config.json file, the URI passed in the parameter has an extra slash (/), three slashes in total.
24   import featureAbility from '@ohos.ability.featureAbility'
25   import ohos_data_ability from '@ohos.data.dataAbility'
26   import ohos_data_rdb from '@ohos.data.rdb'
27
28   let urivar = "dataability:///com.ix.DataAbility"
29   let DAHelper = featureAbility.acquireDataAbilityHelper(urivar);
30   ```
31
322. Construct RDB data.
33
34   ```ts
35   let valuesBucket = {"name": "gaolu"}
36   let da = new ohos_data_ability.DataAbilityPredicates()
37   let valArray =new Array("value1");
38   let cars = new Array({"batchInsert1" : "value1",});
39   ```
40
41   For details about DataAbilityPredicates, see [DataAbility Predicates](../reference/apis/js-apis-data-ability.md).
42
433. Use **insert** to insert data to the DataAbility.
44
45   ```ts
46   // Callback mode:
47   DAHelper.insert(
48     urivar,
49     valuesBucket,
50     (error, data) => {
51       console.info("DAHelper insert result: " + data)
52     }
53   );
54   ```
55
56
57   ```ts
58   // Promise mode (await needs to be used in the asynchronous method):
59   let datainsert = await DAHelper.insert(urivar, valuesBucket).then((data) => {
60     console.info("insert success.");
61   }).catch((error) => {
62     console.error("insert failed.");
63   });
64   ```
65
664. Use **delete** to delete data from the DataAbility.
67
68   ```ts
69   // Callback mode:
70   DAHelper.delete(
71     urivar,
72     da,
73     (error, data) => {
74       console.info("DAHelper delete result: " + data)
75     }
76   );
77   ```
78
79
80   ```ts
81   // Promise mode (await needs to be used in the asynchronous method):
82   let datadelete = await DAHelper.delete(
83     urivar,
84     da,
85   );
86   ```
87
885. Use **update** to update data in the DataAbility.
89
90   ```ts
91   // Callback mode:
92   DAHelper.update(
93     urivar,
94     valuesBucket,
95     da,
96     (error, data) => {
97       console.info("DAHelper update result: " + data)
98     }
99   );
100   ```
101
102
103   ```ts
104   // Promise mode (await needs to be used in the asynchronous method):
105   let dataupdate = await DAHelper.update(
106     urivar,
107     valuesBucket,
108     da,
109   );
110   ```
111
1126. Use **query** to query data in the DataAbility.
113
114   ```ts
115   // Callback mode:
116   DAHelper.query(
117     urivar,
118     valArray,
119     da,
120     (error, data) => {
121       console.info("DAHelper query result: " + data)
122     }
123   );
124   ```
125
126
127   ```ts
128   // Promise mode (await needs to be used in the asynchronous method):
129   let dataquery = await DAHelper.query(
130     urivar,
131     valArray,
132     da
133   );
134   ```
135
1367. Use **batchInsert** to insert data in batches to the DataAbility.
137
138   ```ts
139   // Callback mode:
140   DAHelper.batchInsert(
141     urivar,
142     cars,
143     (error, data) => {
144       console.info("DAHelper batchInsert result: " + data)
145     }
146   );
147   ```
148
149
150   ```ts
151   // Promise mode (await needs to be used in the asynchronous method):
152   let databatchInsert = await DAHelper.batchInsert(
153     urivar,
154     cars
155   );
156   ```
157
1588. Use **executeBatch** to process data in batches in the DataAbility.
159
160   ```ts
161   // Callback mode:
162   DAHelper.executeBatch(
163     urivar,
164     [
165       {
166         uri: urivar,
167         type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
168         valuesBucket: {"executeBatch" : "value1",},
169         predicates: da,
170         expectedCount:0,
171         predicatesBackReferences: null,
172         interrupted:true,
173       }
174     ],
175     (error, data) => {
176       console.info("DAHelper executeBatch result: " + data)
177     }
178   );
179   ```
180
181
182   ```ts
183   // Promise mode (await needs to be used in the asynchronous method):
184   let dataexecuteBatch = await DAHelper.executeBatch(
185     urivar,
186     [
187       {
188         uri: urivar,
189         type: featureAbility.DataAbilityOperationType.TYPE_INSERT,
190         valuesBucket:
191         {
192           "executeBatch" : "value1",
193         },
194         predicates: da,
195         expectedCount:0,
196         predicatesBackReferences: null,
197         interrupted:true,
198       }
199     ]
200   );
201   ```
202
203
204The APIs for operating a DataAbility are provided by **DataAbilityHelper**. For details about the APIs, see [DataAbilityHelper](../reference/apis/js-apis-inner-ability-dataAbilityHelper.md).
205