1

I have saved a database using openOrCreateDatabase in Sqlite at Android Studio. I know that it's saved in the root folder of the Android device (specifically data/data) but I can't see the raw database file.

I tried adb shell command and the Android Device Manager in Studio but I can't see it.

Is there a way to change a database's path or access the database in the data/data without rooting my phone?

BlackEmpiricist
  • 47
  • 3
  • 16

2 Answers2

1

You can't access data folder without rooting device.

But you can copy your database from data folder to SD card for example downloads folder

File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "Database.db");
File currentDB = context.getDatabasePath(DATABASE_NAME);
if (currentDB.exists()) {
    FileChannel src = new FileInputStream(currentDB).getChannel();
    FileChannel dst = new FileOutputStream(backupDB).getChannel();
    dst.transferFrom(src, 0, src.size());
    src.close();
    dst.close();
}

Also you need permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Orest
  • 6,548
  • 10
  • 54
  • 84
  • I have a couple of clarifications (to understand why that code is used) 1.where do i insert the permission? 2. What does src and dst do? 3. Is the sd card the internal storage update device(sorry noob here) 4. Does this code update the backup database you have created? – BlackEmpiricist Jan 12 '15 at 11:23
  • 1. add permission into AndroidManifest.xml file 2. `src` it's input stream with original db and `dst` it's output stream to new copy of db 3. don't understand 4. it just copy your database from app. to some storage nothing else. – Orest Jan 12 '15 at 11:43
  • One last thing, the directory i'm putting the database is in the downloads folder? And can i create a folder to put the dbase in? – BlackEmpiricist Jan 12 '15 at 12:48
  • Yes you can create new folder and then copy your database there, details: http://stackoverflow.com/a/2131051/2055854 – Orest Jan 12 '15 at 12:52
  • I have tried the code inside my oncreate function Why is it that the context in context.getDatabasePath says cannot resolve symbol? And the file input and output and src close and dst closd says: unhandled excpetion java.io.filenotfound and java.io.IOexception – BlackEmpiricist Jan 12 '15 at 14:04
  • 1. `context.getDatabasePath(DATABASE_NAME)` - here you should insert your database name from helper class instead of DATABASE_NAME. 2. You should wrap that code with `try/catch` – Orest Jan 12 '15 at 21:33
  • How to get name from helper class? – BlackEmpiricist Jan 17 '15 at 01:44
  • When you creating database helper class you have constructor where you calling `super(context, DATABASE_NAME, null, DATABASE_VERSION);` here you have your database name. – Orest Jan 17 '15 at 10:23
0

To view the contents in data folder of your app you can use the run command with as as follows

adb shell
$ run-as package.name

Now that you will reach inside the specified application's data folder with all access.

Anonymous Platypus
  • 1,242
  • 4
  • 18
  • 47