0
votes

I am using MvvmCross to create Android, WPF and Windows Phone 8/8.1 apps. I've gotten SQLite working fine in the WPF and Android apps.

With the Windows Phone app, I am running into an issue when calling Create() on the SQLite database file.

The first time this is called, the create and open work just fine, but when the create() is called a second time it always fails.

Code:

ISQLiteConnection db = factory.Create("filename.sql");
db.Close();

Error:

"Could not open database file: filename.sql (CannotOpen)"

Stack:

at SQLite.SQLiteConnection..ctor(String databasePath, Boolean storeDateTimeAsTicks)
at Cirrious.MvvmCross.Plugins.Sqlite.WindowsPhone.MvxWindowsPhoneSQLiteConnectionFactory.Create(String address)
at MvvmCrossPOC.Core.Services.MetadataService.OpenDatabase()

I followed this article, and others with similar steps, and added the MvvmCross SQLite Plugin (v3.5 - Install-Package MvvmCross.HotTuna.Plugin.SQLite), but the error remained.

WP8 SQLite error: The specified module could not be found

Any thoughts on how to move forward?

Code Sample:

public MetadataService(ISQLiteConnectionFactory SQLiteConnectionFactory)
{
    factory = SQLiteConnectionFactory;
}

public List<Platform> GetPlatformCollection()
{
    db = factory.Create(METADATA_REPO_NAME);
    try
    {
        return db.Table<Platform>().ToList();
    }
    finally
    {
        db.Close();
    }
}
1

1 Answers

1
votes

Arg! Hours to figure out this was all me writing the code incorrectly.

Remember: Services are Singletons...so, you don't have to open and close the database every time you use it!

Figured that out after finding Stuart's SQLite example: KittensDB

Not sure why I didn't see this issue on any other platforms...

Working Code Sample

public MetadataService(ISQLiteConnectionFactory factory)
{
    //Get the connection
    db = factory.Create(METADATA_REPO_NAME);

    db.CreateTable<PlatformStorage>();
    db.CreateTable<DashboardStorage>();
}

public List<Platform> GetPlatformCollection()
{
    return db.Table<Platform>().ToList();
}