0

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))
                return;
            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);
            }
            myOutput.Flush();
            myOutput.Close();
            myInput.Close();

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

Dmitry
  • 11
  • 1
  • 5
  • The SQLite.NET library that Xamarin recommends is a very basic ORM that lets you easily store and retrieve objects in the local SQLite database on an Android device.So you can install **sqlite-net-pcl** by Nuget package in your project, there are some article about using it:https://learn.microsoft.com/en-us/xamarin/android/data-cloud/data-access/using-sqlite-orm and https://www.c-sharpcorner.com/article/xamarin-android-sqlite-database/ – Cherry Bu - MSFT Oct 14 '19 at 06:09

1 Answers1

0

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 app.package.name

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

Dmitry
  • 11
  • 1
  • 5