How to store data in a local SQLite.NET database in Xamarin?
6 minute(s) read | Published on: Mar 13, 2021 Updated on: Dec 14, 2021 |
In this tutorial, you will learn:
- How to store data in the local Xamarin database ?
- How to use a NuGet Package Manager to add a NuGet Package? Note: To do this project, you must have already completed the project of building multi-page applications.
How to use local storage in Xamarin Forms using Visual Studio?
1- Open Visual Studio
2- Open the Notes solution project.
3- In the Solution Explorer section, select the Notes project and right-click on it.
4- After right-clicking on the project, select Manage NuGet Packages.
5- In the NuGet Package Manager section, select the Browser tab to search.
6- Search for the SQLite-net-plc. package in NuGet, and after finding it, click Install to add it to the project.
Note:
There are packages with similar names, the package you should choose has the following specifications:
- Author (s): Frank A. Krueger
- Id: SQLite-net-plc.
- NuGet link: SQLite-net-plc.
- Open the Models folder in the Solution Explorer section of the Note. cs project and delete the existing code and enter the following code there.
These codes define the Note model, which stores note data in applications.
Each Note in the SQLite.NET database has its unique id. The ID, PrimeryKey, and AutoIncrement attributes make each Note have its id.
1- Record the changes made and exit that window.
2- Create a Data folder in the Solution Explorer section and add it to the Notes project.
3- In the folder created in the Notes project and the Solution Explorer section, create a class called NoteDatabase.
4- In the NoteDatabase.cs class, delete all existing codes and replace the following codes.
The code written in this class is used to create a database, read data, write and add data and delete it.
Constructor This class receives the address of the file in which the database was created and stored as an argument. The address of the file in which the database was created and stored is provided by the App class. Asynchronous SQLite.NET APIs in these classes transfer database calculations to the background thread.
- Click on the App. xaml.cs section in the Solution Explorer of the Notes projects and replace the code in the file with the code below.
By entering this code, you create a database that creates a NoteDatabase instance as a singleton and receives its filename as an argument and sends it to the NoteDatabase constructor. Creating a database as a singleton has the advantage that only one connection is created and used to communicate with the same connection.
- Save the changes made and exit that window.
- Click on the NotePage.xaml.cs section in the Solution Explorer of the Notes project and replace the code listed below with the OnAppearing method with the code inside the file.
You will collect the notes stored in the database by entering this code. In other words, this code collects all the notes stored in the database.
- Save the changes you made and exit that window.
- Click on NoteEntryPage.xaml.cs in the Solution Explorer section and enter the following code inside it:
NoteEntryPage maintains an instance of a Note.
When the OnSaveButtonClicked method is executed, the Note instance is stored in the database, and the application returns to the previous page.
When the OnDeleteButtonClicked method is executed, the Note instance is removed from the database, and the application returns to the previous page.
Using sqllite.net as local storage in Xamarin Form using Visual Studio for Mac?
1- Open Visual Studio
2- Open the Notes solution project.
3- In the Solution Pad section, select the Notes project and right-click on it.
4- After right-clicking on the project, select Add> Add NuGet Packages.
5- In the NuGet Package Manager section, select the Browser tab to search.
6- Search for NuGet SQLite-net-plc. After finding it, click Add Package to add to the project.
Note:
There are packages with similar names, the package you should choose has the following specifications:
- Author (s): Frank A. Krueger
- Id: SQLite-net-plc.
- NuGet link: SQLite-net-plc.
- Open the Models folder in the Solution Pad section of the Note. cs project and delete the existing code and enter the following code there.
These codes define the Note model, which stores note data in applications.
Each Note in the SQLite.NET database has its unique id. The ID, PrimeryKey, and AutoIncrement attributes make each Note have its id.
- Record the changes made and exit that window.
- Create a Data folder in the Solution Pad and add it to the Notes project.
- In the folder created in the Notes project and the Solution Pad section, create a class called NoteDatabase.
- In the created NoteDatabase.cs class, delete all existing codes and replace the following codes.
The code written in this class is used to create a database, read data, write and add data and delete it.
Constructor This class receives the address of the file in which the database was created and stored as an argument. The address of the file in which the database was created and stored is provided by the App class. Asynchronous SQLite.NET APIs in these classes transfer database calculations to the background thread.
- Click on the App. xaml.cs section in the Solution Pad of the Notes project and replace the code inserted below with the code inside it.
By entering this code, you create a database that creates a NoteDatabase instance as a singleton and receives its filename as an argument and sends it to the NoteDatabase constructor. Creating a database as a singleton has the advantage that only one connection is created and used to communicate with the same connection.
- Save the changes made and exit that window.
- Click on the NotePage.xaml.cs section in the Solution Pad of the Notes project and replace the code listed below with the OnAppearing method with the code inside the file.
By inserting this piece of code, you will collect the notes stored in the database. In other words, this code collects all the notes stored in the database.
- Save the changes you made and exit that window.
- Click on NoteEntryPage.xaml.cs in the Solution Pad and enter the following code inside:
NoteEntryPage maintains an instance of a Note.
When the OnSaveButtonClicked method is executed, the Note instance is stored in the database, and the application returns to the previous page.
About our Android and iOS app development services