1

I have connected my device at system. but i am not able to view my database in file-explore of DDMS. is there any easy way so i can get database file from android-device?

Rahul Rawat
  • 999
  • 2
  • 17
  • 40
  • 1
    In real device is not Possible to see your DB. but you can export your DB in your SDCard and then open in SQLite Browser. – M D Jun 17 '14 at 08:36

4 Answers4

2

You have to follow below step to achieve your task :-

  1. Connect your device and launch the application in debug mode.

  2. Copy the database file from your application folder to your sd card: execute:

    ./adb -d shell "run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite"

  3. Pull the database files to your machine: execute:

    ./adb pull /sdcard/ execute: ./adb

  4. Install Firefox SQLLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/

  5. Open Firefox SQLLite Manager and open your database file from step 3 above.

duggu
  • 37,851
  • 12
  • 116
  • 113
  • How to copy database since data is empty? See [here](http://stackoverflow.com/questions/33560781/how-to-view-sqlite-database-using-real-device) – Hoo Nov 06 '15 at 07:04
1
private void importDB() {
        try {
            File sd = Environment.getExternalStorageDirectory();
            File data = Environment.getDataDirectory();
                if (sd.canWrite()) {
                String currentDBPath = "//data//" + "<package name>"
                        + "//databases//" + "<database name>";
                String backupDBPath = "<backup db filename>"; // From SD directory.
                File backupDB = new File(data, currentDBPath);
                File currentDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Import Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Import Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

private void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();

        if (sd.canWrite()) {
            String currentDBPath = "//data//" + "<package name>"
                    + "//databases//" + "<db name>";
            String backupDBPath = "<destination>";
            File currentDB = new File(data, currentDBPath);
            File backupDB = new File(sd, backupDBPath);

            FileChannel src = new FileInputStream(currentDB).getChannel();
            FileChannel dst = new FileOutputStream(backupDB).getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            dst.close();
            Toast.makeText(getApplicationContext(), "Backup Successful!",
                    Toast.LENGTH_SHORT).show();

        }
    } catch (Exception e) {

        Toast.makeText(getApplicationContext(), "Backup Failed!", Toast.LENGTH_SHORT)
                .show();

    }
}

Here is the reference

Community
  • 1
  • 1
Aniruddha
  • 4,477
  • 2
  • 21
  • 39
0

Go to Android Studio->View->Tool Windows->Device File Explorer->data->data->select your package->databases->you can view your database

Elackya
  • 119
  • 1
  • 13
0

You can use stetho by facebook to debug any android native app.You can achieve that by including stetho in your dependencies.Then initialile stetho in your main application class within the overiden oncreate method once that is done ensure manifest is configured to your application class.You can check https://code.tutsplus.com/tutorials/debugging-android-apps-with-facebooks-stetho--cms-24205

    dependencies {
        compile 'com.facebook.stetho:stetho:1.5.0'
        compile 'com.facebook.stetho:stetho-okhttp:1.5.0'
        } 

then you can use Stetho.initializeWithDefaults(this); to initialize stetho go to chrome://inspect while the app is running then under websql you will be able to see all the tables in your database and write queries to manipulate the database