katsu
katsu

Reputation: 556

How to acces my application database file in android?

I create a application that use SQLite database and insert some data and then I need this full sqlite database file and i try to acces it but I didnot found database file, How to acces this file for use my PC?

Upvotes: 0

Views: 143

Answers (1)

Shrikant Ballal
Shrikant Ballal

Reputation: 7087

If you are using Emulator then you can access your database, just by pulling it out from File Manager from following path: data/data/com.my.package/databases/mydb.sqlite

If you are running your application on your device then use following code to get database from your above filePath (since you cannot access internal file system of android device)

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

            if (sd.canWrite()) {
                String currentDBPath = "/data/com.yourApp.common/databases/yourApp.db";
                String backupDBPath = "/yourApp.db";
                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) {
        }

After that, once you have your db file, you can view it from Mozilla add On (Plugin) called SQLte Manager. Use this: https://addons.mozilla.org/en-us/firefox/addon/sqlite-manager/ to download it.

Upvotes: 2

Related Questions