Data Access

Common Data Access Patterns for Xamarin’s iOS and Android Platforms

PDF for offline use
Sample Code:
Related Recipes:
Related Links:

Let us know how you feel about this


0/250

Most applications have some requirement to save data on the device locally. Unless the amount of data is trivially small, this usually requires a database and a data layer in the application to manage database access. iOS and Android both have the SQLite database engine “built in” and access to store and retrieve data is simplified by Xamarin’s platform. This document shows how to access an SQLite database in a cross-platform way.

Overview

Most applications have some requirement to save data on the device locally. Unless the amount of data is trivially small, this usually requires a database and a data layer in the application to manage database access. iOS and Android both have the Sqlite database engine “built in” and access to the data is simplified by Xamarin’s platform which comes with the SQLite Data Provider.

Xamarin.iOS and Xamarin.Android support database access APIs such as:

  • ADO.NET framework.
  • SQLite-NET 3rd party library.

The majority of the code in this document is completely cross-platform and will run on iOS or Android without modification. There are two sample apps discussed:

  • DataAccess_Basic – Simple data operations writes the results to a text display control;
  • DataAccess_Advanced – Integrates data operations into a small working application that lists and edits a simple data structure.

Both sample solutions contain iOS and Android sample application projects.

For Xamarin.Forms applications, read working with databases which explains how to work with SQLite in a PCL library with Xamarin.Forms.

Sections

iOS

Android

Summary

This chapter discussed data access in Xamarin.iOS and Xamarin.Android using SQLite as the database engine. The database can be accessed “directly” using ADO.NET syntax or you can include the SQLite.NET ORM and perform data operations in C#.

We reviewed two samples: one that contains very simple data access code that outputs to a text field, and a simple application that includes create, read, update and delete functionality. We also discussed threading and how to seed your application with a pre-populated SQLite database.

For additional examples of cross-platform data access see our Tasky Pro case study.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.