Tuesday, September 29, 2015

Access Existing SQLite database in Crossplatform solutions

Background:

I am trying to build out a crossplatform mobile solution using MVVMCross.  Stuart Lodge's N+1 series does a great job explaining how to use a new sqlite database.  The missing use case is how to implement the solution for an existing sqlite database.  I am working on that solution currently.

Problem:

My immediate issue was how to get the Windows Phone sqlite database from the installation into the app storage.

Solution:

private async Task CopyDatabase()
{
    bool isDatabaseExisting = false;
    try
    {
        StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("people.db");
        isDatabaseExisting = true;
    }
    catch
    {
        isDatabaseExisting = false;
    }
    if (!isDatabaseExisting)
    {
        StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("people.db");
        await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
    }
}



Source:

  1. http://wp.qmatteoq.com/using-sqlite-in-your-windows-8-metro-style-applications
  2. http://wp.qmatteoq.com/import-an-already-existing-sqlite-database-in-a-windows-8-application/

No comments:

Post a Comment