How to access a database file on android device [Xamarin App with Entity Framework for SQLite]

android database entity-framework-core sqlite xamarin.forms


I'm beginner in mobile programming, I'm trying to create a database application in Xamarin Forms, there is an Entity Framework SQLite for easy work with the database. After reading the articles I configuring my app so:

public partial class App : Application
        public const string DbFileName = "DailyAppDatabase.db";
        string dbPath = DependencyService.Get<IPath>().GetDatabasePath(DbFileName);

        public App()...

In android project I implemented a method that returns the path to the Database:

return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), filename);

this metod returns "/data/user/0/com.companyname.daily_app/files/DailyAppDatabase.db"

I think I should search a database file here(I want to get this file and open it in SQLite DB Browser). But I can't use emulators, my virtualization system is not working, I debug my app on physical unrooted Android 7.0 device. How I can do this? I've looked at many similar topics like these:

How to access data/data folder in Android device?

Retrieve database or any other file from the Internal Storage using run-as

but found no answer, I really need help, I tried following method:

1) using adb shell command chmod 666 /data/user/0/com.companyname.daily_app/files/DailyAppDatabase.db

2) adb shell -> run-as com.your.packagename -> cp /data/data/com.your.packagename/

But result of these commands on my device is permission denied, I tried this method:

     string _databasePath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DailyAppDatabse.db");      
            if (!File.Exists(_databasePath))
            Stream myInput = new FileStream(_databasePath, FileMode.Open);
            Stream myOutput = Assets.Open("database.db");

            byte[] buffer = new byte[1024];
            int b = buffer.Length;
            int length;
            while ((length = myInput.Read(buffer, 0, b)) > 0)
                myOutput.Write(buffer, 0, length);

But i Think, this is the wrong way to solve the problem, get an exception "method myOutput.write (...) is not supported. Maybe someone has met this problem and can help, I don't want to get a root access and I like code-first approach in EntityFramework Core for SQLite

10/14/2019 7:23:21 PM

Accepted Answer

I found a solution, it was so difficult for me. The following algorithm:

1)making backup file (note the attribute android:allowBackup = true in your app manifest)

adb backup -f "\location\on\local\drive" -noapk

2) unpacking .ab file to .tar file using abe.jar

3) there are files and db file in the .tar folder

maybe this will help someone in the future

10/14/2019 10:43:05 AM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow