0

There is a DBfile created as follows for xamarin code for native android for android sqlite database:

    public static string DBFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),"Test.db");

 public static void InitializeLocalDB()
        {

            if (!File.Exists(DBFile))
            {
                using (var connection = new SQLiteConnection(DBFile))
                {
                    connection.CreateTable<Model.TestItem>();
                }
            }

The model has also been coded.

Model

namespace Test.Model
{
    public class Test
    {
        [PrimaryKey]
        [MaxLength(10)]
        public string Test { get; set; }


    }
}

May i know where is the default location for the database file created from the above code in android phone as i tried using the file manager but cannot find the file?

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
Thomas Koh
  • 213
  • 5
  • 17

2 Answers2

1

The file "test.db" would be within your app's sandbox and thus normally not accessible (unless you are using a rooted device or a non-production image for the emulator )

On Android this:

System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)

Maps to (adjust to your package name of course):

/data/user/0/com.sushihangover.someApp/files/Test.db
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • I have deployed the app into my android device but I do not see the package in Android data folder,how do I create the package in xamarin to access the dB file – Thomas Koh Aug 16 '18 at 10:21
0
/data/data/<Your-Application-Package-Name>/databases/<your-database-name>

as they say in this post.

In physical devices, not emulators, the problem is that, sometimes, depending on the phone, you cannot access that folder. For example i have a normal Android tablet, not rooted. If the app is in Debug (not release), i am able to access that folder from Android Studio Device Explorer.

Sometimes it cannot be accessed that way, for example in certain Samsung phones (it says run-as package is unknown), so i use adb and the method described in this other post.

Edit: Sorry i didnt realize you were asking about Xamarin. Anyway i'll leave you the images from my Android Studio Device File Explorer, just in case it can help you.

enter image description here

enter image description here

Hope it helps.

  • I look at other apps,the packages folder are in /data folder instead of /data/data/package_name How is it done? So my current apps package folder is in in accessible /data/data folder,is my understanding correct? – Thomas Koh Aug 16 '18 at 10:55
  • @ThomasKoh The data (not the app itself) from every app, is inside /data/data/ and inside you can see cache, database, shared_prefs, etc.I just tried in my tablet and took that screenshot. I tried the same in my Samsung and i couldn't see the same, cause for any reason, Samsung doesnt allow it. – Pablo Rodriguez Aug 16 '18 at 15:08
  • I have tried installed android terminal emulator to access the folder,anyone know what is the full path to the /data/data folder? – Thomas Koh Aug 17 '18 at 01:18
  • I have asked another question on how to change the location of the db which maybe another way to get around. https://stackoverflow.com/questions/51887586/how-to-specify-a-specific-location-for-db-file-where-i-can-access-instead-of-sta – Thomas Koh Aug 17 '18 at 02:19
  • You can code a simple method in your app that copies the .db file to an accesible folder of your choice, and from there access it. – Pablo Rodriguez Aug 17 '18 at 02:24