Skip to content Skip to sidebar Skip to footer

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?

Solution 1:

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.

Solution 2:

privatevoidimportDB() {
        try {
            Filesd= Environment.getExternalStorageDirectory();
            Filedata= Environment.getDataDirectory();
                if (sd.canWrite()) {
                StringcurrentDBPath="//data//" + "<package name>"
                        + "//databases//" + "<database name>";
                StringbackupDBPath="<backup db filename>"; // From SD directory.FilebackupDB=newFile(data, currentDBPath);
                FilecurrentDB=newFile(sd, backupDBPath);

            FileChannelsrc=newFileInputStream(currentDB).getChannel();
            FileChanneldst=newFileOutputStream(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();

    }
}

privatevoidexportDB() {
    try {
        Filesd= Environment.getExternalStorageDirectory();
        Filedata= Environment.getDataDirectory();

        if (sd.canWrite()) {
            StringcurrentDBPath="//data//" + "<package name>"
                    + "//databases//" + "<db name>";
            StringbackupDBPath="<destination>";
            FilecurrentDB=newFile(data, currentDBPath);
            FilebackupDB=newFile(sd, backupDBPath);

            FileChannelsrc=newFileInputStream(currentDB).getChannel();
            FileChanneldst=newFileOutputStream(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

Solution 3:

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

Solution 4:

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

Post a Comment for "How To View Sqlite Database In Android Phone"