Let’s see how we can add a Sqlite database to your Xamarin projects in an incredibly easy way.

To help you to speed up your Xamarin development, I’ve created a set of plugins, one of them is MTSQL. Thanks to this plugin you can add a Sqlite database with a single line of code. The plugin is built on top of the Sqlite-net plugin by Frank A. Krueger.

A couple of useful link you can find useful:

Nuget link: https://www.nuget.org/packages/MarcTron.SQL
Project website: http://www.xamarinexpert.it/plugins/mt-sql/
To report any issue: https://bitbucket.org/marcojak81/mtsql

And now let’s see how to integrate the plugin inside your Xamarin Forms solution.

First of all we need to install the plugin. To do that, do a right-click on your solution and click on “Manage NuGet Packages for Solution…”

Now search the package MarcTron.Sqlite, click on it and remember to select all your projects (.Net Standard project + all the main application projects) then click Install.

The Plugin will take care to install for you also the Sqllite-net plugin by Frank A. Krueger.

If everything worked as expected, you will see the version of the plugin next to each of the projects you have selected in the previous step.

Now it’s time to try the plugin to see how easy it is.

Inside the MainPage constructor you can see that the only line of code you need to create your database and open a connection to it is:

SQLiteConnection conn = MTSql.Current.GetConnection("YOURDBNAME.db3");

Remember to replace the string with the name you want for your database.

As you can see in the image after we obtain a connection to the database, we can create a table and add some elements to it.

Just for test I have added 3 items. If everything works as expected we should see inside the text “Rows:3” on screen.

And this is exactly what we get launching the app on UWP.

So, thanks to this plugin, you have your Sqlite database inside your app with only 1 line of code.

What do you think? Add your comment at the end of the page.

3 comments

    User
    Amir
    Reply

    Hi,

    Thank you for your lovely code. It just makes life so much easier!

    But, I have heard I need to save the database inside a different folder on iOS otherwise Apple will reject the app. Is that true?

    If so, could you please advise how I can specify the database path when creating the connection?

    Thanks!

      User
      Marco Troncone
      Reply

      Hi Amir. Thank you for your message. Currently the database path is “SpecialFolder.Personal”. Because the folder belongs to your app, you are allowed to put what you want inside and Apple will not reject the app (I’ve published many apps with this code and Apple was happy about that). I’ll try to update the plugin this weekend so that it will be possible to select a preferred path. Anyway I suggest you to continue to use this path for security reasons.

        User
        Amir
        Reply

        Thank you very much Marco for prompt response and again for your amazing plugin.

        Wouldn’t it be nice to have it published to GitHub as well?

        It’s just so amazing that I can’t image how I have lived without it for so long!

Leave a Reply

%d bloggers like this: