How do I view the SQLite database on an Android device? [duplicate] - android

This question already has answers here:
Debugging sqlite database on the device
(17 answers)
Closed 5 years ago.
I have a set of data in an SQLite database. I need to view the database on a device. How do I do that?
I have checked in ddms mode. The data in file explorer is empty.

Here are step-by-step instructions (mostly taken from a combination of the other answers). This works even on devices that are not rooted.
Connect your device and launch the application in debug mode.
You may want to use adb -d shell "run-as com.yourpackge.name ls /data/data/com.yourpackge.name/databases/" to see what the database filename is.
Notice: com.yourpackge.name is your application package name. You can get it from the manifest file.
Copy the database file from your application folder to your SD card.
adb -d shell "run-as com.yourpackge.name cat /data/data/com.yourpackge.name/databases/filename.sqlite > /sdcard/filename.sqlite"
Notice: filename.sqlite is your database name you used when you created the database
Pull the database files to your machine:
adb pull /sdcard/filename.sqlite
This will copy the database from the SD card to the place where your ADB exist.
Install Firefox SQLite Manager: https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
Open Firefox SQLite Manager (Tools->SQLite Manager) and open your database file from step 3 above.
Enjoy!

UPDATE 2020
Database Inspector (for Android Studio version 4.1). Read the Medium article
For older versions of Android Studio I recommend these 3 options:
Facebook's open source [Stetho library] (http://facebook.github.io/stetho/). Taken from here
In build.gradle:
dependencies {
// Stetho core
compile 'com.facebook.stetho:stetho:1.5.1'
//If you want to add a network helper
compile 'com.facebook.stetho:stetho-okhttp:1.5.1'
}
Initialize the library in the application object:
Stetho.initializeWithDefaults(this);
And you can view you database in Chrome from chrome://inspect
Another option is this plugin (not free)
And the last one is this free/open source library to see db contents in the browser https://github.com/amitshekhariitbhu/Android-Debug-Database

The best way I found so far is using the Android-Debug-Database tool.
Its incredibly simple to use and setup, just add the dependence and connect to the device database's interface via web. No need to root the phone or adding activities or whatsoever. Here are the steps:
STEP 1
Add the following dependency to your app's Gradle file and run the application.
debugCompile 'com.amitshekhar.android:debug-db:1.0.0'
STEP 2
Open your browser and visit your phone's IP address on port 8080. The URL should be like: http://YOUR_PHONE_IP_ADDRESS:8080. You will be presented with the following:
NOTE: You can also always get the debug address URL from your code by calling the method DebugDB.getAddressLog();
To get my phone's IP I currently use Ping Tools, but there are a lot of alternatives.
STEP 3
That's it!
More details in the official documentation:
https://github.com/amitshekhariitbhu/Android-Debug-Database

The best way to view and manage your Android app database is to use the library DatabaseManager_For_Android.
It's a single Java activity file; just add it to your source folder.
You can view the tables in your app database, update, delete, insert rows to you table. Everything from inside your app.
When the development is done remove the Java file from your src folder. That's it.
You can view the 5 minute demo, Database Manager for Android SQLite Database .

You can do this:
adb shell
cd /go/to/databases
sqlite3 database.db
In the sqlite> prompt, type .tables. This will give you all the tables in the database.db file.
select * from table1;

If you are using a real device, and it is not rooted, then it is not possible to see your database in FileExplorer, because, due to some security reason, that folder is locked in the Android system. And if you are using it in an emulator you will find it in FileExplorer, /data/data/your package name/databases/yourdatabse.db.

Try AndroidDBvieweR!
No need for your device to be ROOTED
No need to import the database file of the application
Few configurations and you are good to go!

I have been using SQLite Database Browser to see the content SQLite DB in Android development. You have to pull the database file from the device first, then open it in SQLite DB Browser.

Although this doesn't view the database on your device directly, I've published a simple shell script for dumping databases to your local machine:
https://github.com/Pixplicity/dbdump
It performs two distinct methods described here:
First, it tries to make the file accessible for other users, and attempting to pull it from the device.
If that fails, it streams the contents of the file over the terminal to the local machine. It performs an additional trick to remove \r characters that some devices output to the shell.
From here you can use a variety of CLI or GUI SQLite applications, such as sqlite3 or sqlitebrowser, to browse the contents of the database.

Follow these steps
1>Download the *.jar file from here .
2>Put the *.jar file into the folder eclipse/dropins/ and Restart eclipse.
3>In the top right of eclipse, click the DDMS icon.
4>Select the proper emulator in the left panel.
5In the File Explorer tab on the main panel, go to /data/data/[YOUR.APP.NAMESPACE]/databases.
6>Underneath the DDMS icon, there should be a new blue icon of a Database light up when you select your database. Click it and you will see a Questoid Sqlite Manager tab open up to view your data.
*Note: If the database doesn't light up, it may be because your database doesn't have a *.db file extension. Be sure your database is called [DATABASE_NAME].db
*Note: if you want to use a DB without .db-Extension:
-Download this Questoid SqLiteBrowser: Download fro here.
-Unzip and put it into eclipse/dropins (not Plugins).
-Check this for more information
Click here.

try facebook Stetho.
Stetho is a debug bridge for Android applications, enabling the powerful Chrome Developer Tools and much more.
https://github.com/facebook/stetho

step 1
Copy this class in your package
step 2
put the following code in your class which extends SQLiteOpenHelper.
//-----------------for show databasae table----------------------------------------
public ArrayList<Cursor> getData(String Query)
{
//get writable database
SQLiteDatabase sqlDB =this.getWritableDatabase();
String[] columns = new String[] { "mesage" };
//an array list of cursor to save two cursors one has results from the query
//other cursor stores error message if any errors are triggered
ArrayList<Cursor> alc = new ArrayList<Cursor>(2);
MatrixCursor Cursor2= new MatrixCursor(columns);
alc.add(null);
alc.add (null);
try{
String maxQuery = Query ;
//execute the query results will be save in Cursor c
Cursor c = sqlDB.rawQuery(maxQuery, null);
//add value to cursor2
Cursor2.addRow(new Object[] { "Success" });
alc.set(1,Cursor2);
if (null != c && c.getCount() > 0)
{
alc.set(0,c);
c.moveToFirst();
return alc ;
}
return alc;
}
catch(SQLException sqlEx)
{
Log.d("printing exception", sqlEx.getMessage());
//if any exceptions are triggered save the error message to cursor an return the arraylist
Cursor2.addRow(new Object[] { ""+sqlEx.getMessage() });
alc.set(1,Cursor2);
return alc;
}
catch(Exception ex)
{
Log.d("printing exception",ex.getMessage());
//if any exceptions are triggered save the error message to cursor an return the arraylist
Cursor2.addRow(new Object[] { ""+ex.getMessage() });
alc.set(1,Cursor2);
return alc;
}
}
step 3
register in manifest
<activity
android:name=".database.AndroidDatabaseManager"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar"/>
step 4
Intent i = new Intent(this, AndroidDatabaseManager.class);
startActivity(i);

This works with Android 6.0 (debuggable apps at least):
adb shell "run-as your.package.name cp /data/data/your.package.name/databases/you-db-name /sdcard/file_to_write"
Then you simply can view the DB with aSQLiteManager for instance.

You can try SQLiteOnWeb. It manages your SQLite database in the browser.

Hope this helps you
Using Terminal First point your location where andriod sdk is loacted
eg: C:\Users\AppData\Local\Android\sdk\platform-tools>
then check the list of devices attached Using
adb devices
and then run this command to copy the file from device to your system
adb -s YOUR_DEVICE_ID shell run-as YOUR_PACKAGE_NAME chmod -R 777 /data/data/YOUR_PACKAGE_NAME/databases && adb -s YOUR_DEVICE_ID shell "mkdir -p /sdcard/tempDB" && adb -s YOUR_DEVICE_ID shell "cp -r /data/data/YOUR_PACKAGE_NAME/databases/ /sdcard/tempDB/." && adb -s YOUR_DEVICE_ID pull sdcard/tempDB/ && adb -s YOUR_DEVICE_ID shell "rm -r /sdcard/tempDB/*"
You can find the database file in this path
Android\sdk\platform-tools\tempDB\databases

Using file explorer, you can locate your database file like this:
data-->data-->your.package.name-->databases--->yourdbfile.db
Then you can use any SQLite fronted to explore your database. I use the SQLite Manager Firefox addon. It's nice, small, and fast.

There is TKlerx's Android SQLite browser for Eclipse, and it's fully functional alongside Android Studio. I'll recommend it, because it is immensely practical.
To install it on Device Monitor, just place the JAR file in [Path to Android SDK folder]/sdk/tools/lib/monitor-[...]/plugins.

I found very simple library stetho to browse sqlite db of app in chrome, see

First post (https://stackoverflow.com/a/21151598/4244605) does not working for me.
I wrote own script for get DB file from device. Without root. Working OK.
Copy script to directory with adb (e.g.:~/android-sdk/platform-tools).
Device have to be connected to PC.
Use ./getDB.sh -p <packageName> for get name of databases.
Usage: ./getDB.sh -p <packageName> -n <name of DB> -s <store in mobile device> for get DB file to this (where script is executed) directory.
I recommend you set filename of DB as *.sqlite and open it with Firefox addon: SQLite Manager.
(It's a long time, when i have written something in Bash. You can edit this code.)
#!/bin/sh
# Get DB from Android device.
#
Hoption=false
Poption=false
Noption=false
Soption=false
Parg=""
Narg=""
Sarg=""
#-----------------------FUNCTION--------------------------:
helpFunc(){ #help
echo "Get names of DB's files in your Android app.
Usage: ./getDB -h
./getDB -p packageName -n nameOfDB -s storagePath
Options:
-h Show help.
-p packageName List of databases for package name.
-p packageName -n nameOfDB -s storagePath Save DB from device to this directory."
}
#--------------------------MAIN--------------------------:
while getopts 'p:n:s:h' options; do
case $options in
p) Poption=true
Parg=$OPTARG;;
n) Noption=true
Narg=$OPTARG;;
s) Soption=true
Sarg=$OPTARG;;
h) Hoption=true;;
esac
done
#echo "-------------------------------------------------------
#Hoption: $Hoption
#Poption: $Poption
#Noption: $Noption
#Soption: $Soption
#Parg: $Parg
#Narg: $Narg
#Sarg: $Sarg
#-------------------------------------------------------"\\n
#echo $# #count of params
if [ $Hoption = true ];then
helpFunc
elif [ $# -eq 2 -a $Poption = true ];then #list
./adb -d shell run-as $Parg ls /data/data/$Parg/databases/
exit 0
elif [ $# -eq 6 -a $Poption = true -a $Noption = true -a $Soption = true ];then #get DB file
#Change permissions
./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/
./adb shell run-as $Parg chmod 777 /data/data/$Parg/databases/$Narg
#Copy
./adb shell cp /data/data/$Parg/databases/$Narg $Sarg
#Pull file to this machine
./adb pull $Sarg/$Narg
exit 0
else
echo "Wrong params or arguments. Use -h for help."
exit 1;
fi
exit 0;

Related

how to adb pull folder with wildcard

Basically, I dump all data into the directory 'sdcard/2019-07-xx-xx and there are subdirectories in 2019-07-xx-xx. The Folder is named after the timestamp when dumping data, now I run the command:
adb pull '/sdcard/2019-07*'
but it prompted:
adb: error: failed to stat remote object 'sdcard/2019-07*': No such file or directory
who can help me out?
Example:
for days in 0{1..9} 1{0..9} 2{0..9} 3{0..1}; do
adb shell echo "\$EXTERNAL_STORAGE/2019-07-$days/"
done
Gives us below output:
/sdcard/2019-07-01/
/sdcard/2019-07-02/
/sdcard/2019-07-03/
/sdcard/2019-07-04/
/sdcard/2019-07-05/
......
Final: You can copy and paste
- don't forget to change source_path and dates if needed - Notice the trial slash:
for days in 0{1..9} 1{0..9} 2{0..9} 3{0..1}; do
adb pull "\$EXTERNAL_STORAGE/2019-07-$days/" ~/source_path
done
According to documentation "adb pull" command can pull file or directory from your device. There is no support for a wildcard.
But you can use xargs to accomplish this. Just check post

Androidemulator - Access files in LocalApplicationData

I'm using xamarin forms to develop an Android app. I can save a file via
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), $"appsettiings.txt");
var data = JsonConvert.SerializeObject(this);
File.WriteAllText(fileName, data);
When I debug I can see that the file should be stored at
/data/user/0/<applicationame>/files/.local/share/appsettiings.txt
I like to see if the file is actually saved and what the content is. I opened the Android device monitor but the data folder was 'empty'. From some other SO case I took that I should but myself in root-mode by executing
C:\Program Files (x86)\Android\android-sdk\platform-tools>adb root
Now the data folder contains data and I can drill down up to the files folder, but that one seems to be empty.
When I run the app again and check in code if the file exists, it actually does.
Any further suggestions how to get access to that file from any tooling. I want to delete that file and run the app again.
You can use adb pull to copy the file to local machine and look at it content like this:
adb root
adb pull /data/user/0/<applicationame>/files/.local/share/appsettiings.txt [LOCAL_FOLDER]
then you can use adb shell then rm -f to remove it, like this:
adb shell
su
rm -f /data/user/0/<applicationame>/files/.local/share/appsettiings.txt
Since the file you are requesting is inside /data folder, you need to have the corresponding root priviliege to get it, so you need to do adb root before the adb pull command, and you need to do su before removing the file.
If I am not mistaken, if you save a file with your approach, it should exist, as you already proofed with the .Exists method.
I am not quite sure, are you just trying to read the content out of the file? Or do you want to access it from outside of the application?
If your desire is to just read the content, you could access it with
string content= File.ReadAllText(your file path);
and set a breakpoint (F9) on the next line.

How to view a db file with sqlitebrowser

Recently I want to view a db file with help of a tool. I don't have access to the source code, but after Googling, I found sqlitebrowser may be useful for me. I succeeded in opening db file with it.
My way is:
extract file from android
adb exec-out run-as package.name cat databases/file > file
open the file manually with sqlitebrowser
However it's very troublesome. So I want to write a script to wrap the above two steps. But I do not have any idea how to do it.
Please note I don't want to modify my project
Here is my solution:
adb exec-out run-as package.name cat databases/file > file
/Applications/DB\ Browser\ for\ SQLite.app/Contents/MacOS/DB\ Browser\ for\ SQLite file
The most important is : sqlitebrowser file.We can input file directly.
If you wish to debug content of your database you may consider stetho. I really like those features and it's easy to use.
This nifty library opens sqlite database in your computers web browser,when your app is running. Setup with your project should be straightforward.

Accessing SQLite database from command line

I am trying to access SQLite database from command line for an android application I have developed. I type adb shell to connect to the device and then sqlite3 to connect to the database. sqlite3 gives me the following an error:
/system/bin/sh: sqlite3: not found
Try typing "adb shell type sqlite3" to see whether sqlite3 is available in your device or not.
For my device, it's showing as follows
$ adb shell type sqlite3
--> sqlite3 is a tracked alias for /system/xbin/sqlite3
// Below code works on windows platform.. tried on windows 7 and windows 8
// This is vb script code.. save with .vb extention.
// any problems in saving.. please copy paste any vb file from ur system,take that second file ,open it with notepad and clear all contents, paste the below code as i did same.
// double click to run file. output shown will ur all tables. and u can continue with the own sql statements
// note : works with only one emulator. many emulator are supported.
// please dont try this code with mobile, you wont be able to access mobile database
// start of script
Set WshShell = WScript.CreateObject("WScript.Shell")
// set ur path to platform tools in ur sdk
strApp = "E:\Android\sdk\platform-tools"
arrPath = Split(strApp, "\")
For i = 0 to Ubound(arrPath)
strAppPath = strAppPath & arrPath(i) & "\"
Next
WshShell.CurrentDirectory = strAppPath
WshShell.Run ("cmd.exe")
WScript.Sleep 50
WshShell.SendKeys "adb shell"
WshShell.SendKeys "{ENTER}"
// put ur package name and database name over here. please put ur db name with .db extension
WshShell.SendKeys "sqlite3 /data/data/package_name/databases/database_name"
WshShell.SendKeys "{ENTER}"
WshShell.SendKeys ".tables"
WshShell.SendKeys "{ENTER}"
Set objShell = Nothing
// script ends

How can I copy the contentsof data/ to sdcard/ without using adb?

Hi I need to copy/move the contents of data/tombstones to sdcard/tombstones
I'm using the command below:
mv data/tombstones /sdcard/tombstones
"failed on 'tombstones' - Cross-device link"
but I'm getting above error.
You have a SANE VERSION of the mv command
paraphrasing a few bits from lbcoder from xda and darkxuser from androidforums
"failed on 'tombstones' - Cross-device link"
It means that you can't create a hard link on one device (filesystem) that refers to a file on a different filesystem.
This is an age-old unix thing. You can NOT move a file across a filesystem using most implementations of mv. mv is not made to copy data from device to device, it simply changes a file's location within a partition. Since /data and /sdcard are different partitions, it's failing.
Consider yourself fortunate that you have a SANE VERSION of the mv command that doesn't try anyway -- some old versions will actually TRY to do this, which will result in a hard link that points to NOTHING, and the original data being INACCESSIBLE.
The mv command does NOT MOVE THE DATA!!! It moves the HARDLINK TO
THE DATA.
If you want to move the file to a different filesystem, you need to use the "cp" command. Copy the file to create a SECOND COPY of it on a different filesystem, and then DELETE the OLD one with the "rm" command.
A simple move command:
#!/bin/bash
dd if="$1" of="$2"
rm -f "$1"
You will note that the "cp" command returns true or false depending on the successful completion of the copy, therefore the original will only be removed IF the file copied successfully.
OR
#!/bin/bash
cat data/tombstones > sdcard/tombstones
rm data/tombstones
These script can be copied into some place referenced by the PATH variable and set executable.
Different Interface
If you need a different interface from adb you may move files using the FileExplorer in DDMS View.
Side note:
You can move a file into a folder when:
You're root;
It is your app directory;
You've used chmod from adb or elsewhere to change permissions
Basically you don't have permission to access /data/tombstones in a production version .
It seems we have to 'root' the device first.
But I failed to root my Samsung S4 which is using Android 4.3

Categories

Resources