Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

With MvvmCross what is the preferred way to copy a prefilled SQLite Database

I am modifying the N-10-KittensDb sample solution. I See how to create a SQLite database, but I wish to use an existing database. I am guessing that I need to copy the database to the proper UI data folder. Maybe it is done within the Core project? And if so how is the correct path injected into the running Exec? As the Core can be used across many UI's? What method is called to see if the database exists or needs to be copied?

Sample from DataService:

public DataService(ISQLiteConnectionFactory factory)
{
    const string file = "Cats.sldb";
    var connection = factory.Create(file);
    connection.CreateTable<Kitten>();
}

I believe the paths are different for Android vs Phone vs Touch vs Wpf?

Please direct me to a sample piece of code that uses the Cirrious.MvvmCross.Plugins.Sqlite for Phone or Wpf.

Thank you Dan

like image 216
Cadwell55 Avatar asked Jul 15 '13 23:07

Cadwell55


1 Answers

Each platform by default creates a database in a folder location appropriate for the platform - e.g. Touch uses:

    public ISQLiteConnection Create(string address)
    {
        var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        return new SQLiteConnection(Path.Combine(path, address));
    }

from https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/Sqlite/Cirrious.MvvmCross.Plugins.Sqlite.Touch/MvxTouchSQLiteConnectionFactory.cs#L18

To read/write files, MvvmCross does bundle a File plugin - this also operates by default in platform specific locations - but the two may not match perfectly - e.g. see:

    protected override string FullPath(string path)
    {
        if (path.StartsWith(ResScheme))
            return path.Substring(ResScheme.Length);

        return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), path);
    }

from https://github.com/slodge/MvvmCross/blob/v3/Plugins/Cirrious/File/Cirrious.MvvmCross.Plugins.File.Touch/MvxTouchFileStore.cs#L22

Because of this mismatch, in order to share the same database-specific copy code across platforms you may find it easier to just inject your own platform specific copy on each platform - for more on injecting platform specific services, see http://slodge.blogspot.co.uk/2013/06/n31-injection-platform-specific.html

like image 84
Stuart Avatar answered Oct 10 '22 10:10

Stuart