0
votes

I have my first smartphone since one week and try make a App with Xamarin.

I use SQLite with EntityFrameworkCore to store data.

It is work fine, but to debug easier I want use a SQLite browser. The database file path is 'data/data/{AppName}/Database.db'.

I debug from a physic device by USB, but when I explore the device with Windows Explorer I cannot find the SQLite DB file. The 'data/data' folder is not available. Then I can not use a SQLite browser to see the data.

In this post, the author use a Android emulator and can see 'data/data' folder : https://blog.xamarin.com/building-android-apps-with-entity-framework/

But I prefer use a real device.

Have you a solution?

A solution from the MikeT, in development store the db file in available folder like this :

    public static string DatabasePath
    {
        get
        {
            var dbFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
            var fileName = "database.db";
            var dbFullPath = Path.Combine(dbFolder, fileName);
            return dbFullPath;
        }
    }

In production, copy the db file to a available folder.

2

2 Answers

1
votes

One a real device you would, I believe, need to root the device to directly access the data.

However, what you could do is to copy the database file elsewhere e.g. to external storage. In following is the core process that I use:-

            try {
                    FileInputStream fis = new FileInputStream(dbfile);
                    OutputStream backup = new FileOutputStream(backupfilename);

                    //byte[] buffer = new byte[32768];
                    int length;
                    while((length = fis.read(buffer)) > 0) {
                        backup.write(buffer, 0, length);
                    }
                    backup.flush();
                    backup.close();
                    fis.close();
                catch (IOException e) {
                    e.printStackTrace();
                    confirmaction = false;
                }

I use the following to get the pathname for backupfilename:-

    File dir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),subdirectory);
    this.directory = dir.getPath();

I then add the actual filename (which is user input).

Note! I do checks to determine that EXTERNAL storage is mounted etc.

To get the database path i use:-

    String dbfilename = this.getDatabasePath(
            DBConstants.DATABASE_NAME).getPath();
    dbfile = new File(dbfilename);

This is of course Java, but I'd assume that it could be converted/adapted to suit. The crux of the answer is to get the database file into a place from which you can access it.

0
votes

Call the ExtractDb method from your activity

public void ExtractDB()
        {
            var szSqliteFilename = "databasename.db3";
            var szSourcePath = new FileManager().GetLocalFilePath(szSqliteFilename);

            var szDatabaseBackupPath = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/databasename_Backup.db3";
            if (File.Exists(szSourcePath))
            {
                File.Copy(szSourcePath, szDatabaseBackupPath, true);
                Toast.MakeText(this, "Copied", ToastLength.Short).Show();
            }
        }

Get path to the android device storage as shown below

public class FileManager
    {
        public string GetLocalFilePath(string filename)
        {
            string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            return Path.Combine(path, filename);
        }
    }

you need to add the android.permission.WRITE_EXTERNAL_STORAGE permission in your manifest file.