1

I have been searching for almost a day now and this matter is driving me crazy.

I am trying to open a connection to a SQLite DB I push to the device from windows.

The database now is inside this path:

Phone/Android/data/MyApp.Droid/files/MyDB.db3

which can be read without 'Phone/Android' I think.

When I try to make a connection to the DB I can't seem to find the path.

Here's my code for retrieving the path and creating the connection.

public SQLite.SQLiteConnection GetConnection()
{
    var sqliteFile = "MyDB.db3";
    var dataPath = global::Android.OS.Environment.DataDirectory.AbsolutePath;
    var dataPathExtension = @"MyApp.Droid/files";

    var destination = Path.Combine(dataPath, dataPathExtension, sqliteFile);

    //this outputs the following: /data/MyApp.Droid/files/MyDB.db3
    //When I check my phone this is exactly when I can find the file.

    return new SQLite.SQliteConnection(destination);
    //It can't find the path/file.
}

The DB needs to be in a location I can access from windows without rooting the device.

Can anyone explain to me why it can not find the path/file and if possible tell me how I can read the location?

Note: I cannot access any 'Environment.SpecialFolder' from windows it seems as this gives me a path like: data/user/0/MyApp.Droid/files/

Yours,

Noel Heesen
  • 223
  • 1
  • 4
  • 14
  • Android store files in many weird ways. You should look into "Android resolve file path" or something like this. Depending on your android version, the file type etc this will be different. OR you are lucky and just forgot to store it in ExternalStorage and check the permissions(wich can be internal, external doesn't mean SD card, because... Google doesn't care about logic) – yan yankelevich Nov 04 '16 at 15:58
  • You will have to store the DB file outside of your app's sandbox, Download dir, sdcard, etc.. http://stackoverflow.com/questions/5858107/how-to-get-file-path-from-sd-card-in-android – SushiHangover Nov 04 '16 at 17:05

1 Answers1

1

I use this implementation:

    public SQLiteAsyncConnection GetConnection()
    {
        var fileName = "DatabaseName.db3";
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentsPath, fileName);

        var connection = new SQLiteAsyncConnection(path);

        return connection;
    }

Then I'll grab the .db3 file using Android Device Monitor and the File Explorer inside. You can then open that .db3 file with http://sqlitebrowser.org/ or another browser that supports SQLite. Please note: It's easiest on an Emulator as pulling the files from a physical device can be quite cumbersome without root.

  1. Open Tools -> Android -> Android Device Monitor:

enter image description here

  1. Run your Application in Debug mode
  2. Select your Device in the Devices section:

enter image description here

  1. Pull your .db3 file from data/data/com.mypackage.name/files via the Save Icon:

enter image description here

  1. Open the .db3 file in a SQLite browser

enter image description here

Jon Douglas
  • 13,006
  • 4
  • 38
  • 51