Rahul Rawat
Rahul Rawat

Reputation: 997

How to view sqlite database in android phone

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?

Upvotes: 1

Views: 419

Answers (4)

Zadiki Hassan Ochola
Zadiki Hassan Ochola

Reputation: 778

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

Upvotes: 0

Elackya
Elackya

Reputation: 119

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

Upvotes: 0

Aniruddha
Aniruddha

Reputation: 4487

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

Upvotes: 1

duggu
duggu

Reputation: 38439

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.

Upvotes: 2

Related Questions