LeRoy
LeRoy

Reputation: 4446

Xamarin forms how to Get existing local database

How do you Get an existing database from a device or emulator ?

I'm using Microsoft.WindowsAzure.MobileServices

   public bool InitialiseDb()
    {
        try
        {
            Store = new MobileServiceSQLiteStore(offlineDbPath);
            Store.DefineTable<Products>();
            _client.SyncContext.InitializeAsync(Store);
            this.productTable = _client.GetSyncTable<Products>();

            return true;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
            return false;
        }
    }

Upvotes: 0

Views: 62

Answers (1)

LeRoy
LeRoy

Reputation: 4446

You can copy the existing database into a folder you can access

Create path to database :

 string filepath = "data/data/[package-name]/files/[name-of-db]";

You can get your package name from your android project options here

then use the following code to extract it:

    string filepath = "data/data/com.foo.foo/files/localstorage.db";

    var bytes = System.IO.File.ReadAllBytes(filepath);
    var fileCopyName = string.Format("/sdcard/Database_{0:dd-MM-yyyy_HH-mm-ss-tt}.db", System.DateTime.Now);
    System.IO.File.WriteAllBytes(fileCopyName, bytes);

Upvotes: 1

Related Questions