Migrating a Windows Phone 8.0 app to Xamarin Forms, Part 3 – Using SQLite

(Continuted from the previous post on converting a Windows Phone 8.0 app to Xamarin Forms)

In my WP app, the app’s parking ticket model data was stored in an database table in SQLite. On WP I used Sqlite.Net to allow using LINQ style queries to access and insert data.

In the Xamarin Forms app it’s thankfully mostly the same. However, like in the WP implementation, to access a SQLite database a reference to a SQLiteConnection object is needed. And an instance of ISQLitePlatform needs to be passed to the constructor of the SQLiteConnection class. As this is different on each platform, some code needs to be added to the native Android project in the Xamarin forms solution.

I created an inteface in the Xamarin Forms PCL project which can be consumed by other classes in the PCL project, such as a Data service used by the ViewModels, which return specific model data.

public interface ISQLiteConnectionProvider
{
    SQLiteConnection GetConnection();

    bool IsEmpty { get; }
}

This interface was then implemented in the Android platform project and is responsible for creating the SQLite connection using an instance of SQLitePlatformAndroid. In addition it needs a specific Android database path, in this case using the Android documents folder.

public class SQLiteConnectionProvider : ISQLiteConnectionProvider
{
    private const string DatabaseName = "Tickets.db3";

    public SQLiteConnection GetConnection()
    {
        if (!DatabaseExists)
        {
            IsEmpty = true;
        }

        //Create the connection
        var conn = new SQLiteConnection(
            new SQLitePlatformAndroid(), 
            FullDatabasePath, 
            SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.Create | SQLiteOpenFlags.FullMutex, 
            false);

        return conn;
    }

    public bool DatabaseExists => File.Exists(FullDatabasePath);

    public bool IsEmpty { get; private set; }

    public string FullDatabasePath
    {
        get
        {
            string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); //Documents Folder
            string path = Path.Combine(documentsPath, DatabaseName);
            return path;
        }
    }
}

In the Android platform project, the MainActivity class contains the startup Activity for Android in its OnCreate method I have created an instance of the SQLiteConnectionProvider class and added it to the IoC container (in my app I’m using the Microsoft Unity container as that’s what I’ve used previously).

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity,
        GoogleApiClient.IConnectionCallbacks,
        GoogleApiClient.IOnConnectionFailedListener, 
        Android.Gms.Location.ILocationListener
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);

            global::Xamarin.Forms.Forms.Init(this, bundle);

            SQLiteConnectionProvider sqLiteConnectionProvider = new SQLiteConnectionProvider();
            IocContainer.Instance.RegisterInstance<ISQLiteConnectionProvider>(sqLiteConnectionProvider);

            LoadApplication(this.mainApp);
        }

With this done I’ve got a SQLite database connection which can be used to read and insert data from within my main Xamarin Forms project.

One thought on “Migrating a Windows Phone 8.0 app to Xamarin Forms, Part 3 – Using SQLite

  1. Pingback: Migrating a Windows Phone 8.0 app to Xamarin Forms, Part 4 – Using the Camera | Development Bits and Bobs

Leave a comment