js-apis-system-storage.md 4.5 KB
Newer Older
Z
zengyawen 已提交
1 2 3 4
# 数据存储

> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
>
L
li_juntao 已提交
5
> - 从API Version 6开始,该模块不再维护,可以使用模块[`@ohos.data.storage`](js-apis-data-storage.md)。
Z
zengyawen 已提交
6 7 8 9 10 11 12
>
> - 本模块首批接口从API version 3开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。


## 导入模块


L
li_juntao 已提交
13
```js
Z
zengyawen 已提交
14 15 16 17 18 19 20 21
import storage from '@system.storage';
```


## storage.get

get(Object): void

L
li_juntao 已提交
22
通过key读取缓存中存储的value。
Z
zengyawen 已提交
23 24 25 26 27 28 29 30 31

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 内容索引。 |
| default | string | 否 | key不存在则返回的默认值。 |
L
li_juntao 已提交
32 33
| success | Function | 否 | 接口调用成功的回调函数,返回key对应的value。 |
| fail | Function | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
Z
zengyawen 已提交
34 35 36 37
| complete | Function | 否 | 接口调用结束的回调函数。 |

**示例:**

L
li_juntao 已提交
38
```js
Z
zengyawen 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
export default {    
  storageGet() {        
    storage.get({            
      key: 'storage_key',            
      success: function(data) {                
        console.log('call storage.get success: ' + data);            
      },            
      fail: function(data, code) {                
        console.log('call storage.get fail, code: ' + code + ', data: ' + data);            
      },            
      complete: function() {                
        console.log('call complete');            
      },
    });    
  }
}
```


## storage.set

set(Object): void

L
li_juntao 已提交
62
修改缓存中key对应的value。
Z
zengyawen 已提交
63 64 65 66 67 68 69 70

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 要修改的存储内容的索引。 |
L
li_juntao 已提交
71
| value | string | 是 | 新值。最大长度128。 |
Z
zengyawen 已提交
72
| success | Function | 否 | 接口调用成功的回调函数。 |
L
li_juntao 已提交
73
| fail | Function | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
Z
zengyawen 已提交
74 75 76 77
| complete | Function | 否 | 接口调用结束的回调函数。 |

**示例:**

L
li_juntao 已提交
78
```js
Z
zengyawen 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
export default {    
  storageSet() {        
    storage.set({            
      key: 'storage_key',            
      value: 'storage value',            
      success: function() {                
        console.log('call storage.set success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.set fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}
```


## storage.clear

clear(Object): void

L
li_juntao 已提交
100
清空缓存中存储的key-value键值对。
Z
zengyawen 已提交
101 102 103 104 105 106 107 108

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| success | Function | 否 | 接口调用成功的回调函数。 |
L
li_juntao 已提交
109
| fail | Function | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
Z
zengyawen 已提交
110 111 112 113
| complete | Function | 否 | 接口调用结束的回调函数。 |

**示例:**

L
li_juntao 已提交
114
```js
Z
zengyawen 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
export default {    
  storageClear() {        
    storage.clear({            
      success: function() {                
        console.log('call storage.clear success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.clear fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}
```


## storage.delete

delete(Object): void

L
li_juntao 已提交
134
删除缓存中对应的key-value键值对。
Z
zengyawen 已提交
135 136 137 138 139 140 141 142 143

**系统能力:** SystemCapability.DistributedDataManager.Preferences.Core

**参数:**

| 参数名 | 类型 | 必填 | 说明 |
| -------- | -------- | -------- | -------- |
| key | string | 是 | 内容索引。 |
| success | Function | 否 | 接口调用成功的回调函数。 |
L
li_juntao 已提交
144
| fail | Function | 否 | 接口调用失败的回调函数,data为错误信息,code为错误码。 |
Z
zengyawen 已提交
145 146 147 148
| complete | Function | 否 | 接口调用结束的回调函数。 |

**示例:**

L
li_juntao 已提交
149
```js
Z
zengyawen 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163
export default {    
  storageDelete() {        
    storage.delete({            
      key: 'Storage1',            
      success: function() {                
        console.log('call storage.delete success.');            
      },            
      fail: function(data, code) {                
        console.log('call storage.delete fail, code: ' + code + ', data: ' + data);            
      },        
    });    
  }
}
```