Will you please help me with this problem.
I'm inserting values to my Sqlite database.
How can I check or view the inserted data.
Is there is any tool or other techniques to show the data ?
Will you please help me with this problem.
I'm inserting values to my Sqlite database.
How can I check or view the inserted data.
Is there is any tool or other techniques to show the data ?
If You Want to Show a data on Log
try below code :
for (Contact cn : contacts) {
String log = "Id: "+cn.getID()+" ,Name: " + cn.getName() + " ,Phone: " + cn.getPhoneNumber();
// Writing Contacts to log
Log.d("Name: ", log);
Other Way to show a database
see below steps:
Tools -> DDMS
or click the Device Monitor icon next to SDK Manager in Tool bar.data -> data -> your project name.
After that your databases file will open. Click pull a file from device icon.
Save the file using .db extension.Tools -> SQLiteManager.
If you Download a database on DDMS
then You download
DB.Browser.for.SQLite-3.10.1-win64
and put the database
file on this software and you get the data.
I'll Update My Answer becoz now a days Android Studio update in Android Studio 3.1 or up to other like now 3.2 in this Studio DDMS function is not available but Don't Worry i have another solution.
In your android Studio See right side Bottom Corner there is one Option like Device File Explorer Click on this button
After that you can see like below image is open on your screen :
Now select data -> data -> your project name.
:)
I can't believe no one's mentioned this but what you are probably looking for is Android Debug Database
Works great with Android Room as well and you can view, edit tables and records.
Although you approved Cassius Clay's
answer, if you need to process the raw data
root is not needed ( if it's not on the sdcard follow @Ankit 's answer )
import os
import sys
import subprocess
import sqlite3
import pandas as pd
arg_folder = sys.argv[1] # root folder to recursively search db files from
output_lines = subprocess.check_output(['adb', 'shell', ('ls -R %s' % arg_folder)]).decode('utf-8').splitlines()
db_files = []
current_folder = ''
for line in output_lines:
"""
Output example for 'ls -R /data/data':
/data/data/org.fdroid.fdroid/files/fdroid/repo/icons:
/data/data/org.fdroid.fdroid/shared_prefs:
apks-pending-install.xml
org.fdroid.fdroid_preferences.xml
/data/data/ru.meefik.busybox:
cache
files
lib
shared_prefs
if line contains '/' it's a directory, we want to extract the full path for '.db' files
"""
if line.__contains__('/'):
current_folder = line
elif line.endswith('.db'):
db_files.append('%s/%s' % (current_folder[:-1], line))
print("Listing databases..")
while True:
try:
for idx, full_path in enumerate(db_files):
print("{}) {}".format(idx + 1, full_path))
i = input("Enter database number : ")
db = db_files[int(i) - 1] # selected database
subprocess.check_output(['adb', 'pull', db]) # pulling the .db file from device to local
db = db.split('/')[-1] # "/data/data/com.app/folder/private.db".split('/')[-1] = private
conn = sqlite3.connect(db)
# getting list of current database tables
tables = conn.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall()
for table in tables:
table = table[0] # little fix, it comes as a tuple
print('%s%s' % (db[:-2], table))
with pd.option_context('display.width', 1000): # setting terminal width to view all table columns inline
print(pd.read_sql_query("SELECT * FROM %s" % table, conn)) # print table content
print('- ' * 50) # table print separator
# cleanup
if input('remove database file (from local) [y/N] ? ').lower() == 'y':
print('removing ', db)
os.remove(db)
# repeat
c = input("Press ENTER to continue or CTRL+C to Quit..")
except KeyboardInterrupt:
exit(0)
Here is a less human-friendly very-long-one-liner
read -p "enter root folder to recursively search db files from: " f;dbs=( $(adb shell ls -R $f |
while read line
do
line=$(echo $line | tr -d '\r')
if [[ "$line" =~ ^/.*:$ ]]
then
dir=${line%:}
elif [[ "$line" = "opendir failed" ]]
then
echo "$dir - permission denied"
else
if [[ "$dir" = "/" ]]; then dir=""; fi
echo "$dir/$line" | grep '\.db$'
fi
done)
);echo ${dbs[@]}; dbs+=(exit);select db in "${dbs[@]}"; do
[[ $db == exit ]] && break
echo "You have chosen $db"
adb pull $db
python -c 'import sqlite3;import pandas as pd;db=sqlite3.connect("'${db##*/}'");tables=db.cursor().execute("SELECT name FROM sqlite_master WHERE type=\"table\";").fetchall();print([pd.read_sql_query("SELECT * FROM %s" % t[0], db) for t in tables]);'
done
GIF because I'm too lazy to document
DDMS approach is not working in non rooted device
I follow How to check database on not rooted android device to import the Sqlite DB from device to PC.
Follow the given steps:
adb shell run-as [package] chmod 777 /data/data/[package]/databases/
adb shell run-as [package] chmod 777 /data/data/[package]/databases/[db_file_name]
adb shell run-as [package] cp /data/data/[package]/databases/[db_file_name] /sdcard/
adb pull /sdcard/[db_file_name]
4.Check the pulled SQLite DB in folder sdk>platform-tools
Now you can use any SQLite Viewer to view the pulled database.
Note: Two permissions are required in Manifest file otherwise data will not copied in pulled DB
android.permission.WRITE_EXTERNAL_STORAGE android.permission.READ_EXTERNAL_STORAGE
You can add,edit,check all your DB tables data including your sharedpreferences data with this library (Database debug database) Follow this link to install.
All you need to do is to just put the dependency, compile and run the program ,you will find a link like (your_package_name D/DebugDB: Open http://172.16.6.117:8081 in your browser) in logcat which will show all your DB on browser
The best Sqlite debugging tool for android application is
Stetho By Facebook
http://facebook.github.io/stetho/
Stetho is a sophisticated debug bridge for Android applications. When enabled, developers have access to the Chrome Developer Tools feature natively part of the Chrome desktop browser. Developers can also choose to enable the optional dumpapp tool which offers a powerful command-line interface to application internals.
Download or Alternatively you can include Stetho from Maven Central via Gradle or Maven.
// Gradle dependency on Stetho
dependencies {
compile 'com.facebook.stetho:stetho:1.5.1'
}
<dependency>
<groupid>com.facebook.stetho</groupid>
<artifactid>stetho</artifactid>
<version>1.5.1</version>
</dependency>
Only the main stetho dependency is strictly required, however you may also wish to use one of the network helpers:
dependencies {
compile 'com.facebook.stetho:stetho-okhttp3:1.5.1'
}
Or
dependencies {
compile 'com.facebook.stetho:stetho-okhttp:1.5.1'
}
Or
dependencies {
compile 'com.facebook.stetho:stetho-urlconnection:1.5.1'
}
Integrations
Setup
Integrating with Stetho is intended to be seamless and straightforward for most existing Android applications. There is a simple initialization step which occurs in your Application class:
public class MyApplication extends Application {
public void onCreate() {
super.onCreate();
Stetho.initializeWithDefaults(this);
}
}
This brings up most of the default configuration but does not enable some additional hooks (most notably, network inspection). See below for specific details on individual subsystems.
Enable Network Inspection
If you are using the popular OkHttp library at the 2.2.x+ or 3.x release, you can use the Interceptors system to automatically hook into your existing stack. This is currently the simplest and most straightforward way to enable network inspection.
For OkHttp 2.x
OkHttpClient client = new OkHttpClient();
client.networkInterceptors().add(new StethoInterceptor());
For OkHttp 3.x
new OkHttpClient.Builder()
.addNetworkInterceptor(new StethoInterceptor())
.build();
As interceptors can modify the request and response, add the Stetho interceptor after all others to get an accurate view of the network traffic.
If you are using HttpURLConnection
, you can use StethoURLConnectionManager
to assist with integration though you should be aware that there are some caveats with this approach. In particular, you must explicitly add Accept-Encoding: gzip
to the request headers and manually handle compressed responses in order for Stetho to report compressed payload sizes.
for more info please visit stetho