SQLite has become the accepted industry standard database for mobile apps in the last 10 years, because of its lightweight and open source nature. It is highly portable, since it does not run on server and does not require any major configuration. Just create an SQLite file and bundle it locally with your application and you are ready to roll!
Although it is cross-platform compatible because of its single file format, it has a minor glitch on the Windows based mobile apps. Android and iOS have built-in support for SQLite through their native libraries, while some programming is involved in making SQLite run with Windows apps.
The SQLite engine exposes a C++ API that can be accessed through .Net by using a C# wrapper. Any third party API that provides a higher-level C# API for this purpose can be used. There are several options like SQLite Mono, which has easy .Net coding resources. However, its performance is hampered because Windows support is not available in addition to the usual Android and iOS support. There is a similar predicament with Microsoft library offering for SQLite, as there is no adequate documentation.
Methods of using SQLite in Xamarin
When coding with Xamarin, SQLite.Net is the ORM (Object Relational Mapping) API that allows accessing the database objects without writing SQL statements. In this tutorial, experts of xamarin app development India will help you to focus on how to integrate SQLite.Net with the mobile platforms.
There are basically two ways of doing this:
- Use NuGet
The SQLite.Net PCL NuGet package supports all 3 platforms – iOS, Android, and Windows.
- Use Component Store
Xamarin component store has SQLite.Net available for iOS and Android only.
Hence, when developing for all the 3 major platforms, it makes sense to use the NuGet package.
Setting up SQLite .Net in Xamarin
iOS, Android, and Windows (after configuration)
The following points walk you through the process of using SQLite .Net, which is the same for both approaches:
- Add a using statement
usingSQLite;
- Create a blank database
vardb=newSQLiteConnection (dbPath);
- Save data
db.CreateTable<Stock> (); db.Insert (newStock); // after creating the newStock object
- Retrieve data
var stock =db.Get<Stock>(10); // primary key id of 10varstockList=db.Table<Stock>();
Configuring SQLite .Net
For Windows, there is an additional process of activating the SQLite runtime. When you download the SQLite .Net PCL NuGet package in Xamarin, install the component authored by Frank Krueger. This will create two packages:
- SQLite-net-PCL
This is the core assembly that contains code to identify the data entities and high level classes that create queries to access those entities.
- raw
This provides the C# wrapper around the C++ APIs exposed by SQLite. This is used internally by the SQLite-net-PCL package to access the database. Unless you wish to use the API programmatically, it is not required to code for this package.
Special instructions for Windows
In case of Windows though, where native library support for accessing SQLite is not available, it is necessary to activate the SQL runtime. This database engine is written in C++ and included as a binary in the raw package. In order for it to function, there is a dependency on visual C++ runtime.
For this, a reference needs to be added in the system.
- In the section of Windows Phone in the Xamarin Studio, open the References.
- Go to extensions.
- Select Visual Studio C++ 2013 runtime. (Please note that even though later versions are available, only this runtime should be used as the raw package is compiled against it. This applies for Windows 8, 8.1, and uwp as well.)
This step is crucial as a type initialization error is encountered if this reference is not added, and the SQLite will not be able to load the runtime DLL.
Now that you have covered setting up SQLite.Net on all 3 platforms, you are set to use the lightweight database in Xamarin.
Rosina De Palma
Latest posts by Rosina De Palma (see all)
- Why Enterprise Mobility Solutions Is Important in the Banking Industry - July 5, 2022
- Use Your Great Optimization Skills To Get Found In App Store - July 6, 2017
- This Is What Developers Should Know About iOS 10 - June 12, 2017