Most of the Applications that we develop, needs to have a local database to have some important information saved on the device, it’s important that, we only try to save information that is really necessary and locally required in our device, so our app always have the best performance possible.
In this post, we will be learning how to implement a local database in our App. For this example will create a Contacts App.
First of all… What do I need?
Add from NuGet Package the plugin: Sqlite-net-pcl
Step 1⃣: Identifying the Entities
You must identify the Entities and the attributes that must be hosted in the App, this case in the Contact class.
⚠ Important: You need to add a PrimaryKey to seed each register with a unique presence.
Step 2⃣: Creating the Database
The Database will be created with a Singleton design pattern. A single database object (and connection) allowing us to keep the database open while the app is running. If you want more information about it, you can enter here.
The Database class gets a string path as parameter which is composed of the following data:
- The folder location
- The database name. (In this case “Contacts.db3”)
In the App.xaml.cs class, add the following code:
Step 3⃣: Using the database
To use the database, we must establish the connection. Then we will handle the DB as we want. We have a lot of methods that makes the database management easier such as: Save, delete, update, among others.
Step 4⃣: Making the action
Step 5⃣: Creating the screen design
In the following example I added edit features. A mask in the telephone entry. If you want to know how to do it, you can enter here.
Good luck with your implementation!
Click to see an example in my GitHub.
Thank for reading!!! 💚