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:

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

  1. 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:

  2. Exercise 2: Add SQLite.Net

    In this exercise, you will add the SQLite.NET component to each project in your solution using NuGet.

  3. 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.

  4. 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.

What about Entity Framework? Guide on Cross-Platform Data Access

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.

Deploying a database file with a Xamarin.Forms app Working with the iOS File System (Xamarin) iOS File System Basics (Apple Developer Documentation) Browsing the File System in Android (Xamarin) Storage Options in Android (Google) Data access in UWP (MSDN)

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.

SQLite-Net PCL on NuGet SQLite home page Source Code for SQLite-Net PCL Extensions for Foreign Key relationships Efficiently inserting multiple records in SQLite