0

My App has a database. Now I would like to copy the database for backup to standard user folder or SD Card. In Eclipse I find it at data/data/database - but where is the database on the real device?

vaultah
  • 44,105
  • 12
  • 114
  • 143
Chad White
  • 309
  • 1
  • 4
  • 15

4 Answers4

0

The path showed by Eclipse is right, the absolute path change by device, if you have rooted the device you can see the file. Are always in /data/data/*. If you device is not rooted you cant see this files

Wuilfor
  • 26
  • 2
0

IN REAL Devices you can't access those files!!!

Exceptional
  • 2,994
  • 1
  • 18
  • 25
  • That's wrong. You can't access to the directory from your Explorer but you have access programmatically. – Namenlos Aug 16 '13 at 11:21
  • Ofcourse only if u have root permission.. Check this out.. http://stackoverflow.com/questions/7268908/access-the-phone-internal-storage-to-push-in-sqlite-database-file – Exceptional Aug 16 '13 at 11:25
  • You have access without root permissions too. Look at my answer. – Namenlos Aug 16 '13 at 11:30
0

try this... just replace lite.db it is name of database of mine.

private void copyDB() {
    File dir = new File(Environment.getExternalStorageDirectory()
            + "/backup");
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File from = new File("/data/data/" + getPackageName() + "/databases/",
            "lite.db");
    File to = new File(dir, "lite.db");
    try {
        FileInputStream in = new FileInputStream(from);
        FileOutputStream out = new FileOutputStream(to);
        FileChannel fromChannel = null, toChannel = null;
        try {
            fromChannel = in.getChannel();
            toChannel = out.getChannel();
            fromChannel.transferTo(0, fromChannel.size(), toChannel);
        } finally {
            if (fromChannel != null)
                fromChannel.close();
            if (toChannel != null)
                toChannel.close();
        }
    } catch (IOException e) {
        Log.e("backup", "Error backuping up database: " + e.getMessage(), e);
    }

}

Also not forget to add permission:

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
sandy
  • 3,311
  • 4
  • 36
  • 47
0

The database is stored in the devices data directory, you can get it with Environment.getDataDirectory(). In this directory your database is stored under the following path: /data/YOUR.PACKAGE.NAME/databases/YOUR.DB.NAME.

Here is a little example how you can backup your database:

public void exportDB() {
    try {
        File sd = Environment.getExternalStorageDirectory();
        File data = Environment.getDataDirectory();
        if(sd.canWrite()) {
            String currentDBPath = "//data//com.example.packagename//databases//" + DB_NAME;
            String backupDBPath = DB_NAME;
            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) {
        e.printStackTrace();
    }
}

Of course, you also need <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

After that, you can find your database in your SD with the filename given by "DB_NAME".

Namenlos
  • 1,615
  • 2
  • 18
  • 24