6

I have quite a specific question.

First let me explain what I am trying to do and then the problem.

Basically what I am trying to do is to store an existing .db database file in assets folder and then after installation I want to copy the .db file from assets to the default database location in Android. Something similar to this page answer [link]How to use an existing database with an Android application.

Problem For unrooted device I cannot access /data/data/<<package name folder>> . In this case where is the database file gets stored. In other term I want to copy a file from assets to applications default location for database which is DB_PATH = "/data/data/" + context.getPackageName() + "/databases/" in case of a rooted device. Would it be the same for the unrooted device.

Community
  • 1
  • 1
user2129794
  • 2,388
  • 8
  • 33
  • 51

3 Answers3

9

Yeah, For both the cases it will be same path. /data/data/<application_package_name>/databases

Now, on un-rooted device you can not access /data/ directory of device's internal storage. That's why you can not seen the database file.

If you want to get the file you can copy database file from internal storage /data/data/<application_package_name>/databases to external storage (sdcard) then using ddms or adb pull get the database file.

Also just try command adb pull /data/data/<application_package_name>/databases/<database_file_name> from your system to get the database file.

But by default all the android application store database on internal storage path /data/data/<application_package_name>/databases. And its applicable for all devices rooted or un-rooted.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I agree I cannot see the directory in an unrooted device but can I perform operation assuming the directory `/data/data//databases` exists. I want to copy a file from **assets** folder to the databases folder. Can I copy assuming the same directory path for rooted as well as unrooted device. – user2129794 Mar 10 '13 at 19:32
  • Yes, you can not seen the file, but you can access the database file within your application and perform any operation on it. As file already physically present there. – user370305 Mar 10 '13 at 19:34
  • is there any app that can fetch db file from this location? – Bugs Happen Jun 24 '16 at 05:28
  • 1
    This answer is not correct anymore. For years I have been using this directory for copying databases in and out of the app, today for some reason databases were stored in /data/user/0//databases This directory can be retrieved by context.getDatabasePath(String name) as posted below by cja. – redocoder Jul 28 '17 at 10:06
  • Worth noting that within Android Studio, the "Device File Explorer" can be used to retrieve the database from the device; however, it is still located within /data/data//databases. – BluJ IT Jul 27 '19 at 18:05
8

According to Android docs at http://developer.android.com/reference/android/content/Context.html#getDatabasePath(java.lang.String) you should use context.getDatabasePath.

Works for me:

File dbFile = context.getDatabasePath(name_of_database_file);
cja
  • 9,512
  • 21
  • 75
  • 129
0

By Default Android Stores its SQLite Database to this below link, where you can access your .db file by using shell from command prompt to get owner access using chmod for read or write permission.

String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;

Where:
String DATABASE_NAME = "your_dbname";
String PACKAGE_NAME = "com.example.your_app_name";

For more details you can check the following link. Click here

Community
  • 1
  • 1
Crime_Master_GoGo
  • 1,641
  • 1
  • 20
  • 30