• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Data Storage
2
3>  **NOTE**
4>
5>  - The APIs of this module are no longer maintained since API version 6, and you are advised to use [`@ohos.data.storage`](js-apis-data-storage.md).
6>
7>  - The initial APIs of this module are supported since API version 3. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9
10## Modules to Import
11
12```js
13import storage from '@system.storage';
14```
15
16## storage.get
17
18get(options: GetStorageOptions): void
19
20Reads the value stored in the cache based on the specified key.
21
22**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
23
24**Parameters**
25
26| Name | Type                   | Mandatory| Description      |
27| ------- | -------------------- | ---- | ---------- |
28| options | [GetStorageOptions](#getstorageoptions) | Yes  | API configuration.|
29
30**Example**
31
32```js
33export default {
34  storageGet() {
35    storage.get({
36      key: 'storage_key',
37      success: function(data) {
38        console.log('call storage.get success: ' + data);
39      },
40      fail: function(data, code) {
41        console.log('call storage.get fail, code: ' + code + ', data: ' + data);
42      },
43      complete: function() {
44        console.log('call complete');
45      },
46    });
47  }
48}
49```
50
51## storage.set
52
53set(options: SetStorageOptions): void
54
55Sets the value in the cache based on the specified key.
56
57**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
58
59**Parameters**
60
61| Name | Type                  | Mandatory| Description      |
62| ------- | ------------------- | ---- | ---------- |
63| options | [SetStorageOptions](#setstorageoptions) | Yes  | API configuration.|
64
65**Example**
66
67```js
68export default {
69  storageSet() {
70    storage.set({
71      key: 'storage_key',
72      value: 'storage value',
73      success: function() {
74        console.log('call storage.set success.');
75      },
76      fail: function(data, code) {
77        console.log('call storage.set fail, code: ' + code + ', data: ' + data);
78      },
79    });
80  }
81}
82```
83
84## storage.clear
85
86clear(options?: ClearStorageOptions): void
87
88Clears the key-value pairs from the cache.
89
90**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
91
92**Parameters**
93
94| Name | Type                                       | Mandatory| Description          |
95| ------- | ------------------------------------------- | ---- | -------------- |
96| options | [ClearStorageOptions](#clearstorageoptions) | No  | API configuration.|
97
98**Example**
99
100```js
101export default {
102  storageClear() {
103    storage.clear({
104      success: function() {
105        console.log('call storage.clear success.');
106      },
107      fail: function(data, code) {
108        console.log('call storage.clear fail, code: ' + code + ', data: ' + data);
109      },
110    });
111  }
112}
113```
114
115## storage.delete
116
117delete(options: DeleteStorageOptions): void
118
119Deletes the key-value pair based on the specified key.
120
121**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
122
123**Parameters**
124
125| Name | Type                                         | Mandatory| Description          |
126| ------- | --------------------------------------------- | ---- | -------------- |
127| options | [DeleteStorageOptions](#deletestorageoptions) | Yes  | API configuration.|
128
129**Example**
130
131```js
132export default {
133  storageDelete() {
134    storage.delete({
135      key: 'Storage1',
136      success: function() {
137        console.log('call storage.delete success.');
138      },
139      fail: function(data, code) {
140        console.log('call storage.delete fail, code: ' + code + ', data: ' + data);
141      },
142    });
143  }
144}
145```
146
147## GetStorageOptions
148
149**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
150
151| Name    | Type         | Mandatory| Description                    |
152| -------- | ---------------- | ---- | ------------------- |
153| key      | string                               | Yes  | Key of the target data.                                     |
154| default  | string                               | No  | Default value returned when the specified key does not exist.                |
155| success  | (data: any) => void                  | No  | Called to return the result when **storage.get()** is called successfully. **data** is the value indexed by the specified key. |
156| fail     | (data: string, code: number) => void | No  | Called to return the result when **storage.get()** fails to be called. **data** is the error information, and **code** indicates the error code. |
157| complete | () => void                           | No  | Called when **storage.get()** is complete.     |
158
159
160## SetStorageOptions
161
162**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
163
164| Name    | Type               | Mandatory| Description                  |
165| -------- | ------------------- | ---- | -------------------- |
166| key      | string                               | Yes  | Key of the data to set.                                |
167| value    | string                               | Yes  | New value to set. The length must be less than 128 bytes.                             |
168| success  | () => void                           | No  | Called when **storage.set()** is called successfully.                     |
169| fail     | (data: string, code: number) => void | No  | Called to return the result when **storage.set()** fails to be called. **data** is the error information, and **code** indicates the error code. |
170| complete | () => void                           | No  | Called when **storage.set()** is complete.                              |
171
172
173## ClearStorageOptions
174
175**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
176
177| Name    | Type            | Mandatory| Description                        |
178| -------- | --------------------- | ---- | -------------------- |
179| success  | () => void                           | No  | Called when **storage.clear()** is called successfully.                |
180| fail     | (data: string, code: number) => void | No  | Called to return the result when **storage.clear()** fails to be called. **data** is the error information, and **code** indicates the error code. |
181| complete | () => void                           | No  | Called when **storage.clear()** is complete.                         |
182
183
184## DeleteStorageOptions
185
186**System capability**: SystemCapability.DistributedDataManager.Preferences.Core
187
188| Name    | Type                | Mandatory| Description                 |
189| -------- | -------------------- | ---- | ------------------ |
190| key      | string                               | Yes  | Key of the data to delete.                                            |
191| success  | () => void                           | No  | Called when **storage.delete()** is called successfully.               |
192| fail     | (data: string, code: number) => void | No  | Called to return the result when **storage.delete()** fails to be called. **data** is the error information, and **code** indicates the error code. |
193| complete | () => void                           | No  | Called when **storage.delete()** is complete.                        |
194
195