0

I've already vainly searched extensively to find a method to access db without rooting my android phone. I don't want to use AVD as it's very slow. But in order to see what I'm doing I need to access my app .db file from PC or phone in someway.

/data/data//databases/.db doesn't work for real devices.

So, please suggest me some method by which I can see my database.

Shivam Mangla
  • 92
  • 6
  • 16
  • It is actually possible for you own applications see http://stackoverflow.com/questions/4856210/access-sqlite-database-on-android-device/14193376#14193376 – bwt May 07 '13 at 12:36

4 Answers4

2

Create a function in your app to copy the DB from the internal (protected) memory to external (unprotected) such as an SD card. Then you can access it with your PC (or even an Android DB reader from the device itself if you want to).

Barak
  • 16,318
  • 9
  • 52
  • 84
  • I tried this.. private static final String DATABASE_NAME = Environment.getExternalStorageDirectory().getPath(); But it doesn't work.. can u please give me the piece of code? – Shivam Mangla Jan 11 '13 at 19:08
  • Amrut: Please enlighten me more. Show me the code or tell me the method. I can't google now. I've been searching 4 last 2 days & I've not found anything good that works. – Shivam Mangla Jan 11 '13 at 19:27
2

Just add code in your application on specific event it will copy the DB into SD card, it will be copy of ur DB/data not actual DB. From SD card you can always access the DB. this is the work around but it works for me.

Here is the code

    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if (sd.canWrite()) {
            String currentDBPath = "data/"+sPackageName+"/databases/"+sDBName;
            String backupDBPath = "/.appname-external-data-cache/"+sDBName; //"{database name}";
            File dir = new File(sd,backupDBPath.replace(sDBName,""));
            if(dir.mkdir()) {

            }
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);
            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();
            }
        } 

   } catch (Exception e) {

    }
Amrut
  • 543
  • 4
  • 8
0

Here is the code I think you are after:

try {
                // Backup of database to SDCard
                db.open();
                File newFile = new File(Environment.getExternalStorageDirectory(), "BackupFileNameHere");
                InputStream input = new FileInputStream("/data/data/com.YourProjectName/databases/DatabaseNameHere");
                OutputStream output = new FileOutputStream(newFile);

                // transfer bytes from the Input File to the Output File
                byte[] buffer = new byte[1024];
                int length;
                while ((length = input.read(buffer))>0) {
                    output.write(buffer, 0, length);
                }
                output.flush();
                output.close();
                input.close();
                db.close();

            } catch (Exception e) {
            }

I hope this helps.

Brian White
  • 307
  • 1
  • 2
  • 11
  • Brian: Thanks for the help. But your method doesn't work. It shows the same error as it shows when I try to store my data in sdcard itself in the first place. – Shivam Mangla Jan 12 '13 at 13:23
  • Can you post your error(catlog)? I have this code in most of my projects and it works great. I use it on a button onclick. If you want send me an email and I will send you the whole class where I use this code it might help you. Email Address is roman.appsllc@gmail.com – Brian White Jan 12 '13 at 18:03
0

We just set the file permissions to readable for all users from within the app.

if (BuildConfig.DEBUG)
{
     new File(mDB.getPath()).setReadable(true, false);
}

Then just get the .db with adb -pull

adb -d pull //data/data/xxxxx/databases/xxxxx.db .

NOTE: I've discovered that this needs to be done each time the database file is opened, for example in onCreate as well as the constructor of your SQLiteOpenHelper wrapper (when your database is not null) or perhaps onOpen. If only in onCreate, then the next time you run your app and the .db already exists, for some reason the permissions have been changed back. This might have something to do with how Android manages its data.

delrocco
  • 495
  • 1
  • 4
  • 23