How to check database on not rooted android device
You can write your database to the external memory with the following:
private void writeToSD() throws IOException {
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
String currentDBPath = DB_NAME;
String backupDBPath = "backupname.db";
File currentDB = new File(DB_PATH, 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();
}
}
}
Where DB_NAME
is the name of my database and DB_PATH
is defined as follows:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
DB_PATH = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator;
}
else {
DB_PATH = context.getFilesDir().getPath() + context.getPackageName() + "/databases/";
}
And add the following permission (Thanks to @Sathesh for pointing this out):
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I call this method anytime I have a database write so that my most current database file is in the external memory and I can view it and debug from there.
Then you can use the X-Plore app to view the database from the external memory right on the Android device.
How to access data/data folder in Android device?
Accessing the files directly on your phone is difficult, but you may be able to copy them to your computer where you can do anything you want with it.
Without rooting you have 2 options:
If the application is debuggable you can use the
run-as
command in adb shelladb shell
run-as com.your.packagename
cp /data/data/com.your.packagename/Alternatively you can use Android's backup function.
adb backup -noapk com.your.packagename
You will now be prompted to 'unlock your device and confirm the backup operation'. It's best NOT to provide a password, otherwise it becomes more difficult to read the data. Just click on 'backup my data'. The resulting 'backup.ab' file on your computer contains all application data in android backup format. Basically it's a compressed tar file. This page explains how you can use OpenSSL's zlib command to uncompress it.
You can use theadb restore backup.db
command to restore the backup.
Access database without rooting
Just add code in your application on specific event it will copy the DB into SD card, it will be copy of ur DB/data not actual DB. From SD card you can always access the DB.
this is the work around but it works for me.
Here is the code
try {
File sd = Environment.getExternalStorageDirectory();
File data = Environment.getDataDirectory();
if (sd.canWrite()) {
String currentDBPath = "data/"+sPackageName+"/databases/"+sDBName;
String backupDBPath = "/.appname-external-data-cache/"+sDBName; //"{database name}";
File dir = new File(sd,backupDBPath.replace(sDBName,""));
if(dir.mkdir()) {
}
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) {
}
Get database file in /data/data on rooted device
cat source_file > dest_file
You can also install busybox, and get the use of cp command, along with a host of others.
https://play.google.com/store/apps/details?id=stericson.busybox&hl=en
How to view database on root device ?
you can copy your sqlite database with adb
from your device
- connect your phone to pc (usb, debug enabled...)
adb devices
list your connected phonesadb -s deviceName pull /data/data/your.app.name.space/databases/your-db-name.db
if you don't know hows the name of your db use the adb shell
adb -s deviceName shell
than list the files in your databases folder like this
ls /data/data/your.app.name.space/databases/
than you have the sqlite database you can browse with some sqlite tools
Can a non-rooted phone android user see/edit the data stored in Realm DB?
If the user will be able to view the database depends on where you have saved the database. If you are not modifying the location of the database, then by default the realm database is stored at the same location as SQLite db. So, the user should not be able to view the file.
However, if you are placing the database in a custom folder accessible without root (like sdcard) the user will be able to view it.
For the safety purpose, you can encrypt the database. You can read more about it here
How to root an android device / How to get Sqlite Database from Real Unrooted device
You can't get SQLite databse file from an Android device that is not rooted, since you have no permission to access data folder.
But you can run your application in an android emulator and you can get your database file from there (this is the easiest and safest method of all).
Rooting process is safe if it is done right, and gives you full access to all features and stuff on your device. It is not safe when you don't know what to do with all that freedom :)
Here you can find more about rooting
Related Topics
Install_Failed_Cpu_Abi_Incompatible on Device Using Intellij
Is Google-Services.JSON Safe from Hackers
Animation for Expandablelistview
Using Espresso to Unit Test Google Maps
Is Main Thread The Same as UI Thread
Android: Radio Button in Custom List View
Android - Creating a New Activity in Eclipse
Export The Contacts as Vcf File
Restarting a Device Programmatically
How to Get The Application's Icon from The Package Name
No Resource Found That Matches The Given Name '@Style/ Theme.Holo.Light.Darkactionbar'
Actionlayout on Menuitem Does Nothing
Detecting Webview Error and Show Message
Android Ble Connection Time Interval
How to Get Thumbnail for Video in My /Sdcard/Android/Data/Mypackage/Files Folder