Intermediate: Data Management (Relational Database) in Harmony OS
Introduction
Huawei provides various services for developers to make ease of development and provides best user experience to end users. In this article, we will cover Relational database (RDB) with Java in Harmony OS.
A relational database (RDB) is a common type of database that stores data in tables, so it can be used in relation to other stored data sets. Relational database is a digital database based on relational models. RDB use SQLite, which is a standard user application that provides an easy programming interface for database interaction. To satisfy different needs, the RDB offers a list of methods for adding, querying, Updating, and managing data, and to supports direct execution of SQL statements. In a relational database, each row in the table is a record with a unique ID called the key. The columns of the table hold attributes of the data, and each record usually has a value for each attribute, making it easy to establish the relationships among data points.
Limitations and Constraints
- To ensure data accuracy, the RDB supports only one write operation at a time.
- The RDB can use a maximum four connection pools to manage read and write operations.
Development Overview
You need to install DevEcho studio IDE and I assume that you have prior knowledge about the Harmony OS and java.
Hardware Requirements
- A computer (desktop or laptop) running Windows 10.
- A Huawei phone (with the USB cable), which is used for debugging.
Software Requirements
- Java JDK installation package.
- DevEcho studio installed.
Follows the steps.
- Create HarmonyOS Project.
- Open DevEcho studio.
- Click NEW Project, select a Project Templet.
- Select ability template and click Next as per below image.
- Enter Project and Package Name and click on Finish.
2. Once you have created the project, DevEco Studio will automatically sync it with Gradle files. Find the below image after synchronization is successful.
3. Update Permission and app version in config.json file as per your requirement, otherwise retain the default values.
4. Create New Ability, as follows.
5. Development Procedure.
To create the database.
Add the below code in StoreDataSlice.java
Add the below code in ReadDataAbility.java
Add the below code in RDBItemProvider.java
Add the below code in ability_read_data.xml
6. To build apk and run in device, choose Build > Generate Key and CSR Build for Hap(s)\ APP(s) or Build and Run into connected device, follow the steps.
Result
- Click on UI ‘Store Data in DB’ button. It will navigate to Store data screen as per below images.
2. Enter data and click on “Save data” button. It will store data in RDB as per below images.
Pros: It is easy to use, store data in local memory without internet connection
Cons: The database uses tables having rows and columns which consumes a lot of physical memory which becomes a disadvantage of the database.
3. Click on “Fetch Data DB” button, we will read data from RDB as per below images.
Pros: It is easy to use, fetch data from local memory without internet connection.
Cons: It will work locally a single user can access.
3. Click on “Delete Data DB” button, we will read data from RDB as per below images.
Pros: We can delete database easily and clear consumed memory.
Cons: After deleting data it’s very difficult to retrieve same data.
Tips and Tricks
- Always use the latest version of DevEcho Studio.
- Use Harmony Device Simulator from HVD section.
Conclusion
In this article, we have learnt Relational database (RDB) in Harmony OS. The RDB offers a list of methods for querying, deleting, updating, and adding data, and supports direct execution of SQL statements. The HarmonyOS RDB provides a complete mechanism for managing local databases.
Thanks for reading the article, please do like and comment your queries or suggestions.
References
Harmony OS: https://www.harmonyos.com/en/develop/?ha_source=hms1
Lightweight Preference database:
Original Source :-https://forums.developer.huawei.com/forumPortal/en/topic/0201617388341320098?ha_source=hms1