1# Data Storage<a name="EN-US_TOPIC_0000001152064139"></a> 2 3- [Introduction](#section117mcpsimp) 4- [Directory Structure](#section124mcpsimp) 5- [Constraints](#section128mcpsimp) 6- [Usage](#section134mcpsimp) 7 - [Available APIs](#section136mcpsimp) 8 - [Permissions]( #section137mcpsimp) 9 10- [Usage Guidelines](#section163mcpsimp) 11 - [Parameters of the Insert API](#section1099113151207) 12 - [Parameters of the Delete API](#section1098113151208) 13 - [Parameters of the Update API](#section1099113151207) 14 - [Parameters of the Query API](#section1096113151208) 15 - [Sample Code](#section1558565082915) 16 17- [Repositories Involved](#section206mcpsimp) 18 19## Introduction<a name="section117mcpsimp"></a> 20 21The data storage module stores persistent data of key modules of the Telephony subsystem, such as the SIM cards and SMS modules, and provides the **DataAbility** API for data access. 22 23**Figure 1** Architecture of the data storage module<a name="fig13267152558"></a> 24 25![](figures/en-us_architecture-of-the-data-storage-module.png) 26 27## Directory Structure<a name="section124mcpsimp"></a> 28 29``` 30/base/telephony/data_storage # Data storage 31├─ BUILD.gn # Build script (gn) 32├─ README.md # Readme 33├─ common # Public and common files 34│ ├─ include 35│ └─ src 36├─ opkey # opkey framework 37│ ├─ include 38│ └─ src 39├─ pdp_profile # Network carrier 40│ ├─ include 41│ └─ src 42├─ sim # SIM card 43│ ├─ include 44│ └─ src 45├─ sms_mms # SMS/MMS 46│ ├─ include 47│ └─ src 48├─ ohos.build # Build code 49└─ test # Test code 50``` 51 52## Constraints<a name="section128mcpsimp"></a> 53 54- Programming language: C++ 55 56- Software constraints: This module must work with the Utils and Application Framework subsystems. 57 58- Hardware constraints: none 59 60- Use case: When a user needs to edit the persistent data stored in Telephony subsystem modules, such as the SIM card and SMS/MMS modules, your application can call the **Insert**, **Delete**, **Update**, and **Query** APIs provided by **DataAbilityHelper** as demanded. 61 62 You need to declare the corresponding permission for access to the specified URI. 63 64## Usage<a name="section134mcpsimp"></a> 65 66### Available APIs<a name="section136mcpsimp"></a> 67 68**Table 1** APIs provided by DataAbilityHelper 69 70<a name="table165976561598"></a> 71 72| API Definition | **Description**| 73| ------------------------------------------------------------ | ------------ | 74| int Insert(const Uri &uri, const NativeRdb::ValuesBucket &value) | Inserts data. | 75| int Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates) | Deletes data. | 76| int Update( const Uri &uri, const NativeRdb::ValuesBucket &value, const NativeRdb::DataAbilityPredicates &predicates) | Updates data. | 77| std::shared_ptr\<NativeRdb::AbsSharedResultSet\> Query( const Uri &uri, const std::vector\<std::string\> &columns, const NativeRdb::DataAbilityPredicates &predicates) | Queries data. | 78 79**Table 2** Required permissions 80 81<a name="table165976561598"></a> 82 83| Module | **Required Permission** | 84| -------------- | ------------------------------------------------------------ | 85| SMS/MMS module | ohos.permission.READ_MESSAGES | 86| SIM card module | ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE | 87| Network carrier module| ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE | 88| opkey module| ohos.permission.GET_TELEPHONY_STATE<br>ohos.permission.SET_TELEPHONY_STATE | 89 90## Usage Guidelines<a name="section163mcpsimp"></a> 91 92### Parameters of the Insert API<a name="section1099113151207"></a> 93 94**Table 3** Parameters of the Insert API 95 96<a name="table1234838197"></a> 97 98| Parameter | **Description** | 99| ----- | ------------------------------------- | 100| uri | Resource path. | 101| value | Data set. This field corresponds to the table structure field on which the current operation is performed.| 102 103### Parameters of the Delete API<a name="section1098113151208"></a> 104 105**Table 4** Parameters of the Delete API 106 107<a name="table1234838197"></a> 108 109| Parameter | Description | 110| ---------- | ------------------------------------- | 111| uri | Resource path. | 112| value | Data set. This field corresponds to the table structure field on which the current operation is performed.| 113| predicates | Conditions for data deletion. | 114 115### Parameters of the Update API<a name="section1097113151210"></a> 116 117**Table 5** Parameters of the Update API 118 119<a name="table1234838197"></a> 120 121| Parameter | Description | 122| ---------- | -------- | 123| uri | Resource path.| 124| predicates | Conditions for data updating.| 125 126### Parameters of the Query API<a name="section1096113151208"></a> 127 128**Table 6** Parameters of the Query API 129 130<a name="table1234838197"></a> 131 132| Parameter | Description | 133| ---------- | -------------- | 134| uri | Resource path. | 135| columns | Fields in the query result.| 136| predicates | Conditions for data query. | 137 138### Sample Code<a name="section1558565082915"></a> 139 140The following provides the procedure and sample code for you to query, insert, delete, or update SMS/MMS data: 141 1421. Call **SystemAbilityManagerClient** to obtain a **SystemAbilityManager** object. 1432. Call **saManager** to obtain an **IRemoteObject** object based on the specified service ID. 1443. Call **IRemoteObject** to create a **DataAbilityHelper** object. 1454. Call **DataAbilityHelper::Query** to query data, and call the other related APIs to process data. 146 147 Sample code for creating a **DataAbilityHelper** instance: 148 ``` 149 std::shared_ptr<AppExecFwk::DataAbilityHelper> CreateDataAHelper(int32_t systemAbilityId) 150 { 151 // Obtain a SystemAbilityManager instance through SystemAbilityManagerClient. 152 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 153 if (saManager == nullptr) { 154 DATA_STORAGE_LOGE("DataSimRdbHelper Get system ability mgr failed."); 155 return nullptr; 156 } 157 // Obtain an IRemoteObject. 158 auto remoteObj = saManager->GetSystemAbility(systemAbilityId); 159 while (remoteObj == nullptr) { 160 DATA_STORAGE_LOGE("DataSimRdbHelper GetSystemAbility Service Failed."); 161 return nullptr; 162 } 163 // Create a DataAbilityHelper instance. 164 return AppExecFwk::DataAbilityHelper::Creator(remoteObj); 165 } 166 ``` 167 Sample code for querying SMS/MMS messages: 168 ``` 169 std::shared_ptr<NativeRdb::AbsSharedResultSet> SmsSelect(std::shared_ptr<AppExecFwk::DataAbilityHelper> helper) 170 { 171 // Resource path 172 Uri uri("dataability:///com.ohos.smsmmsability/sms_mms/sms_mms_info"); 173 // Fields in the query result 174 std::vector<std::string> colume; 175 // Phone number of the message sender 176 colume.push_back("sender_number"); 177 // Message title 178 colume.push_back("msg_title"); 179 // Message content 180 colume.push_back("msg_content"); 181 // Query predicates 182 NativeRdb::DataAbilityPredicates predicates; 183 // Call the DataAbilityHelper::Query API to query data. 184 return helper->Query(uri, colume, predicates); 185 } 186 ``` 187 Sample code for inserting SMS/MMS messages: 188 ``` 189 int SmsInsert(std::shared_ptr<AppExecFwk::DataAbilityHelper> helper) 190 { 191 Uri uri("dataability:///com.ohos.smsmmsability/sms_mms/sms_mms_info"); 192 NativeRdb::ValuesBucket value; 193 // Phone number of the message recipient 194 value.PutString(SmsMmsInfo::RECEIVER_NUMBER, "138XXXXXXXX"); 195 // Message content 196 value.PutString(SmsMmsInfo::MSG_CONTENT, "ceshi"); 197 value.PutInt(SmsMmsInfo::GROUP_ID, 1); 198 return helper->Insert(uri, value); 199 } 200 ``` 201 Sample code for deleting SMS/MMS messages: 202 ``` 203 int SmsDelete(std::shared_ptr<AppExecFwk::DataAbilityHelper> helper) 204 { 205 Uri uri("dataability:///com.ohos.smsmmsability/sms_mms/sms_mms_info"); 206 NativeRdb::DataAbilityPredicates predicates; 207 // Delete the message whose MSG_ID is 1. 208 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1"); 209 return helper->Delete(uri, predicates); 210 } 211 ``` 212 Sample code for updating SMS/MMS messages: 213 ``` 214 int SmsUpdate(std::shared_ptr<AppExecFwk::DataAbilityHelper> helper) 215 { 216 Uri uri("dataability:///com.ohos.smsmmsability/sms_mms/sms_mms_info"); 217 NativeRdb::ValuesBucket values; 218 // Message content 219 values.PutString(SmsMmsInfo::MSG_CONTENT, "hi ohos"); 220 NativeRdb::DataAbilityPredicates predicates; 221 // Message ID 222 predicates.EqualTo(SmsMmsInfo::MSG_ID, "1"); 223 return helper->Update(uri, values, predicates); 224 } 225 ``` 226 227 228## Repositories Involved<a name="section206mcpsimp"></a> 229 230[Telephony Subsystem](https://gitee.com/openharmony/docs/blob/master/en/readme/%E7%94%B5%E8%AF%9D%E6%9C%8D%E5%8A%A1%E5%AD%90%E7%B3%BB%E7%BB%9F.md) 231 232**telephony_data_storage** 233 234[telephony_core_service](https://gitee.com/openharmony/telephony_core_service/blob/master/README.md) 235 236[telephony_sms_mms](https://gitee.com/openharmony/telephony_sms_mms/blob/master/README.md) 237 238[telephony_cellular_call](https://gitee.com/openharmony/telephony_cellular_call/blob/master/README.md) 239 240[telephony_call_manager](https://gitee.com/openharmony/telephony_call_manager/blob/master/README.md) 241