SQLite and Mobile Data (XAM160)
Many apps need to store data locally on the device so it is available even when network access is unreliable. User preferences, data files, and relational data can all fall into this category. This course starts with a survey of the local-storage options available to you in your Xamarin applications. It then concentrates on storing relational data using a SQLite database. By the end of the course, you will have seen how to identify the proper location for your database file and how to insert, update, retrieve, and delete data efficiently using asynchronous I/O calls.
Upon completion you will be able to:
- Understand the available data storage options
- Create, Insert, Update, and Delete records with SQLite
- Work with SQLite asynchronously
Course exercise download
Exercise materials are available from GitHub. The code can be cloned through a desktop Git client or downloaded directly as a ZIP file.
If you are using Windows, try to select a location with a shorter path length to make sure you don't exceed the 260 character limit.
Lecture
Here are the lecture slides.
Exercises
-
Exercise 1: Determine your database file path
In this exercise, you will create a new Xamarin.Forms application and determine the location to store a database file on each platform. We are going to initially display the full path to where we want to store the file as a
Label
as shown here: -
Exercise 2: Add SQLite.Net
In this exercise, you will add the SQLite.NET component to each project in your solution using NuGet.
-
Exercise 3: Access a SQLite database with SQLite.Net
In this exercise, you will define the database schema for a SQLite database and then create and connect to the database.
-
Exercise 4: Access a SQLite database using asynchronous methods
In this exercise, you will update an existing project and modify the synchronous database calls to use their asynchronous counterparts from the SQLite.NET library.
Additional resources
Additional Resources
Check out the common questions that people ask about SQLite and storing data locally on the device.
Working with the file system
In this class we will be utilizing SQLite.Net to store relational data, however we will be storing it locally on the device's file system. We aren't diving deep into the local file system itself, other than to show where the database file should be placed, however you can check out other resources if you want to store other types of files.
Using SQLite-Net PCL
We will be using the SQLite.NET ORM to access SQLite databases on the device. Here are some additional resources you might find valuable.