Export SQLite database from your android devices
On Android, if you want to access the SQLite database on device, you can use adb to cat it.
adb shell
run-as com.example.sample
cat databases/SampleDB
It works pretty well, if you just want to check some text or row numbers. But it doesn’t give you the full view of the database.
To download the full database, you have to do something extra:
- Add permissions for read/write external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
- Add the code to export the SQLite database from private data to SD card
protected void exportDb() {
File externalStorageDirectory = Environment.getExternalStorageDirectory();
File dataDirectory = Environment.getDataDirectory();
FileChannel source = null;
FileChannel destination = null;
String currentDBPath = "/data/" + getApplicationContext().getApplicationInfo().packageName + "/databases/SampleDB";
String backupDBPath = "SampleDB.sqlite";
File currentDB = new File(dataDirectory, currentDBPath);
File backupDB = new File(externalStorageDirectory, backupDBPath);
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (source != null) source.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (destination != null) destination.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- Call it somewhere
- Download the file from SD card. Personally I prefer WiFi File Transfer, after it start, just open the http://YOUR_ID_ADDRESS:1234 to download the files. :D