DotNet MAUI Sqlite Tutorial - .Net MAUI Sqlite CRUD Operations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Welcome! In this video, we'll explore the  utilization of a local database within a .net maui application. A local database refers to a  database that is entirely housed within the device and can be accessed locally. For our demonstration,  we'll use the sqlite database as the local database. To begin, I have created a dotnet maui project in Visual  Studio named MauiSqliteDemo. Before delving into the development process, it's essential to note  that this video is part of our MAUI tutorial series. For additional tutorials and related content, you  can access the entire playlist through the link provided in the video description. To leverage  the sqlite database, it's necessary to install two libraries from nuget packages. The initial Library  required is sqlite-net-pcl, which can be found listed at the top. Let's proceed with  the installation. The second essential library is "SqlitePclRaw.bundle_green". Let's also  install this library to complete the setup. Next, let's incorporate the main page as a dependency in  our "MauiProgram.cs" class. I'm incorporating it as a transient dependency service. With this dependency  injected, we can now assign the main page property in the "app.xaml.cs" class. By obtaining the main  page object through the class constructor, we can designate it as the initial page for our  application. Moving forward, I'm creating a class called LocalDbService within our project. This  class will encompass the necessary methods for interacting with our sqlite database. To begin, I'm  declaring a private constant string to represent the database name. Following that, I'm creating a  readonly object of the SqliteAsyncConnection named connection. within the class constructor,  let's instantiate the connection object by providing the database path as a parameter. This  path is constructed by compiling the app data directory and the specified database name. Before  proceeding further, let's create an entity class for the database table. The class will be named  "customer". I'm removing the unnecessary using statements to streamline the code. Let's provide  the needed properties. The first being an ID property. Additionally, we'll incorporate properties  for customer name, mobile and email within the customer class. In Sqlite, it's possible to assign a  custom table name if necessary. To achieve this, we can utilize the table attribute provided  by the Sqlite library. For the ID property, I'm incorporating attributes such as primary key,  Auto increment and specifying a custom column name. using the column name property. Similarly I'll  assign custom column names for customer name, mobile and email properties within this customer  class. Returning to the local DB service class constructor, we can now include the method for  creating the customer table in the sqlite database. Moving on to the implementation of the methods  required for CRUD operations. Firstly, the method for fetching all customers which will return a list  of customers. This can be achieved by returning the table of customers from the sqlite database. Following  that the get by ID method will return a single customer record based on the provided ID. Utilizing  the "Where" method on the table of customers, we can filter the data with the provided ID and return  the result. For the create method, used to insert a customer record, we can use the InsertAsync  method of the connection object. Next is the update method facilitating the modification of an  existing customer data. Leveraging the UpdateAsync method of the connection object for this purpose.  Lastly the delete method can be implemented using the DeleteAsync method of the connection object.  The implementation of the local DB service class is now complete with the incorporation of these  methods. Shifting focus to the "MauiProgram.cs" class. We'll proceed to inject the LocalDbService as a  singleton dependency service. Let's move to the main page. I'll be presenting a straightforward design  to display a list of customers incorporating add, edit and delete operations. I won't delve too  deeply into the design aspects as these have been thoroughly covered in our previous videos.  Within this layout, you'll notice entry fields for name, email and mobile. Additionally, there's  a save button provided for data saving during addition or editing. To enhance user interaction,  the list view have has been configured with an ItemTapped event which will be utilized to trigger  actions such as edit or delete upon tapping a list item. Proceeding to the "MainPage.cs" class. I'll  initiate the implementation of the functionalities. Firstly, I'm declaring a private readonly object of  our local DB service class. Since we injected the LocalDbService as a dependency, we can acquire the  class object from the constructor and instantiate it. I'll also assign the item source property of the  list view by retrieving all customers from our LocalDbService. As the method is awaitable, I'll  utilize "Task.Run" to execute it asynchronously. Continuing with the implementation, I'll declare  an integer variable to store the ID during the editing process. Upon a user clicks on the save  button, we'll check whether the value of this variable is zero. If it is, we'll proceed to  add a new customer. Otherwise, we'll execute the update process. To facilitate the addition of  a new customer, we can utilize the Create method of our db service. The method will take  a new customer object as a parameter and we'll populate its properties such as customer  name, email and mobile with the values obtained from the corresponding entry fields. To implement  the editing of a customer, we can use the update method. It's crucial to include the ID value in the  customer object parameter which can be retrieved from the editCustomerId variable. Similar to  the process of adding a customer the remaining properties can be assigned in the same manner.  After a successful update, we'll set the value of editCustomerId to zero. Upon the completion  of adding or updating a customer, we can enhance the user experience by clearing the entry fields.  Additionally, we can reload the list ensuring that the changes are promptly reflected in the UI. To  execute functionalities when a user taps on a list item, I'll begin by casting the customer object  from the event argument's item property. Following this, an action sheet will be displayed, allowing  the user to select an action. Edit or Delete. I'll implement a switch-case to handle different tasks  based on the user selection. For the edit case, upon selecting the edit action, I'll assign the value  of the editCustomerId variable to the ID of the customer object. Furthermore, I'll populate  the entry fields for name, email and mobile with the respective values from the customer  object. In the delete case, upon selecting the delete action, the delete method of our LocalDbService  will be invoked. Subsequently, the list of customers in the list view will be reloaded for  immediate reflection of changes. That concludes the necessary implementations of our main page.  Now let's proceed to test our application by running it on an Android Emulator. The app is up  and running. Let's attempt to add a new customer by providing the name, email and mobile details.  Upon clicking the save button, you can observe the seamless addition of the customer. Promptly  appearing in the listing below. Let's proceed to add another customer for a further  demonstration. As expected, the new customer has been successfully added to the list. By tapping  on a list item, an action sheet is displayed offering edit and delete actions. Let's proceed  to edit this customer for a demonstration. Upon selection, the customer data is populated in the  Entry Fields. Let's modify the values by providing a different name and email. Upon clicking the save  button, the list promptly refreshes showcasing the updated values. Now let's explore the process of  deleting a customer. Upon initiating the deletion, you'll notice the customer swiftly removed  from the list. This confirms the seamless functionality of all the implemented methods.  I hope this tutorial has been informative and helpful in enhancing your understanding of local  database integration in .NET MAUI. If you have any questions or suggestions, please feel free to leave  your comments below. Don't forget to like, share and subscribe to our channel for more insightful  content. Thank You for joining us and Happy Coding!
Info
Channel: Coding Droplets
Views: 2,967
Rating: undefined out of 5
Keywords: net maui sqlite crud, .net maui sqlite crud, maui app to sqlite, maui sqlite, dotnet maui sqlite, maui sqlite tutorial, how to use sqlite in .net maui, .net maui sqlite tutorial, maui sqlite entity framework, maui sqlite android, .net maui sqlite android, maui sqlite crud, sqlite crud in .net maui, .net maui sqlite ios, maui sqlite ios, .net maui sqlite entity framework, maui sqlite connection, maui sqlite multiple tables, maui sqlite foreign key, maui sqlite-net-pcl
Id: VziMUc-VQko
Channel Id: undefined
Length: 9min 36sec (576 seconds)
Published: Tue Nov 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.