I know I have previously written about how to store data on your Android device using the Xamarin platform but there are a few different methods for data storage that you want to consider based on your app’s needs. Using the SharedPreferences section of the device is not always appropriate or feasible and we need a more robust option for storing data.
Why Use a Database?
There are a number of advantages to using an SQL database in your mobile app:
- SQL databases allow efficient storage of structured data.
- Specific data can be extracted with complex queries.
- Query results can be sorted.
- Query results can be aggregated.
- Developers with existing database skills can utilize their knowledge to design the database and data access code.
- The data model from the server component of a connected application may be re-used (in whole or in part) in the mobile application.
I would also add that while many developers think that using a SQL server is a more robust option, we have to think about app performance and reliability when the cellular connection is bad or the server is unavailable. There are also a lot of security implications of using synchonized and decentrialized storage and we want to be sure that we are accessing and transmitting data securely, which is not something that new developers really think about. By storing the data on the phone we eliminate some of the security risks we could be facing by moving that data to a server.
What is SQLite?
SQLite is an in-process library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine. The code for SQLite is in the public domain and is thus free for use for any purpose, commercial or private. You can learn more about SQLite here.
One of the main benefits of using SQLite for Android apps is that the SQLite engine has been adopted by Google for the mobile platform. It is also very easy to use with the SQLite .NET tools that are available to us through the Xamarin platform.
SQLite is well suited to cross-platform mobile development because:
- The database engine is small, fast and easily portable.
- A database is stored in a single file, which is easy to manage on mobile devices.
- The file format is easy to use across platforms: whether 32- or 64-bit, and big- or little-endian systems.
- It implements most of the SQL92 standard.
It is very easy to get started using SQLite on your Android apps. Here is my video that walks you through setting up your project and doing some basic data operations so you can get started using databases in your apps right now.