Search code examples
sqlitexamarin.formssqlite-net-pcl

SQLite-Net-Pcl in Xamarin.Forms


I am trying to learn SQLite and its usage from Xamarin.Forms. I found that it is recommended to install Sqlite-net-pcl as NuGet package and make use of it to create, open, modify a database. Up to that point everything is fine. However, I am struggling to find more details about Sqlite-net-pcl on the web. It seems, a comprehensive documentation is not available for this library. And this reality brought some questions to my mind which I believe I could get answer from Stackoverflow seniors:

1-If there is no documentation of a library which you are trying to use, what is the best way to learn it quickly? Trial&Error? In that case full class hierarchy would be needed, I think. 2-As I understood, Sqlite-net-pcl is a partial implementation of Sqlite. For instance, in the documentation of Sqlite, it is said, Foreign key is supported but should be enabled. But, I couldnt find this attribute in Sqlite-net-Pcl when I used the ObjectBrowser tool of VisualStudio.
3-Considering, SQLite have better support, If I want to use SQLite directly in xamarin.forms(c#) instead of its derived versions(Sqlite-net-pcl) how can I achieve this?


Solution

  • It is preferable to use SQLite-net libraries. They are simply a wrapper around SQLite flow. You have everything that you need there. Also, if you want to use an ORM, you can also use EntityFrameworkCore via Microsoft.EntityFrameworkCore.Sqlite

    Like it is said in the GitHub repository:

    SQLite-net is an open source, minimal library to allow .NET, .NET Core, and Mono applications to store data in SQLite 3 databases. It was first designed to work with Xamarin.iOS, but has since grown up to work on all the platforms (Xamarin.*, .NET, UWP, Azure, etc.).

    About the Foreign Key - there isn't any constraints. You can use it freely without any "enabling".

    There are some other options like Microsoft.Data.SQLite & System.Data.SQLite, but I haven't tried them with Xamarin and, if they work, I don't think that they will have better support for Mono, like the one that you have with SQLite-net. The latter is also updated constantly.

    Here is the official tutorial from Microsoft about using sqlite-net-pcl

    If you want to use an ORM, you can follow this article from the official docs on how to setup the SQLite libraries, together with EFCore packages, migrations, etc.

    Should you choose to use EFCore, please consider the Migration Limitations.