debug android : connect to sqlite db on phone - android

I'm building an application with a SQLite db in it. Some data is not erased as it should be, I'd like to run ad hoc queries in the existing db to debug.
Is there a way to connect to the SQLite db on the phone (or the emulator) while in debug mode?

Yes, you can connect to your device (or the emulator) using this manual.
Use adb to connect to the emulator:
$ adb -s emulator-5554 shell
and then launch the sqlite3 application with the database you need:
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
Then you can run any SQL query you want.

You can use the sqlite3 command to open the database. If you have a rooted device (works on the emulator too), use su to become root and get access to private files. If not, just make your app debuggable and use run-as to open the DB. Something like:
run-as my.package sqlite3 /data/data/my.package/databases/my.db

Related

How to find database file generated of my application in android device

I made an application which collects information of device and create a database using SQLite in android. Using file explorer, i can see database of my application in Emulator using Eclipse. I attached the image as shown.
I want to collect information of my android device. I execute the application in my device. Now i am not able to find neither package name of my application nor database of my application.
Please help?
You will only be able to access the database on a physical device if your device is rooted. An emulator gives full access to the databases, where as a device does not, unless it is rooted.
You can try shelling into the device from the command line over USB like so:
# normal shell on local machine
adb shell
# android shell on device
run-as [your.package.name.here]
cd databases
sqlite3 [database_name]
Note that on some devices you do not have access to sqlite3 from the shell like this. In those cases I have copied the database file to the external storage directory (usually /sdcard), exited the shell, then used adb pull to retrieve it.
# android shell on device
cp [database_name] /sdcard
exit
# normal shell on local machine
cd [somewhere]
adb pull /sdcard/[database_name] .

How to Access Database on Android Emulator Device

I'm developing an Android application and using SQLite.
I would like to access the database and run some SQL queries on the emulator handset for debugging purpose. How to do this?
You can use the sqlite3 command. Read here http://developer.android.com/guide/developing/tools/adb.html#shellcommands
To use sqlite3, enter a remote shell on the emulator instance, as
described above, then invoke the tool using the sqlite3 command.
Optionally, when invoking sqlite3 you can specify the full path to the
database you want to explore. Emulator/device instances store SQLite3
databases in the folder /data/data//databases/.
Example:
$ adb -s emulator-5554 shell
# sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.3.12
Enter ".help" for instructions
.... enter commands, then quit...
sqlite> .exit

How to access sqlite DB on the Xoom?

I have a Nexus One (rooted) and a Xoom (stock, not rooted).
I have developed on my Nexus for quite some time, without any problems. I'm facing one with the Xoom, however, as it is not rooted.
Indeed, I use sqlite databases in my apps, and I would like to debug them using sqlite3 as I do on my Nexus.
It doesn't work!
$ pwd
/
$ id
uid=2000(shell) gid=2000(shell) groups=1003(graphics),1004(input),1007(log),1009(mount),1011(adb),1015(sdcard_rw),3001(net_b_admin),3002(net_bt),3003(inet)
$ sqlite3 /data/data/org.bicou.newsreader/databases/subscriptions.db
sqlite3: permission denied
I can't! How come? How am I supposed to develop? I'm sure I'm missing something but I don't know what.
Also, adb pull doesn't want to pull, and cat / cp / mv don't work either. I don't want to write specific code in my app just to browse the DB...
If it is your app you're debugging, there is no need to root the device.
Edit this list of commands to your package name and db file name, and paste it into a shell:
adb wait-for-device shell <<EOF
run-as [YOUR_PNAME]
chmod 666 databases/[YOUR_DB_FILE_NAME].db
exit
exit
EOF
adb pull /data/data/[YOUR_PNAME]/databases/[YOUR_DB_FILE_NAME].db
sqlitebrowser [YOUR_DB_FILE_NAME].db &
The sqlitebrowser can be replaced with whatever sqlite3 application you have on your pc.
I rooted my device as well. But new system image doesn't have sqlite3 in path.
I ended up rooting the device.
(answser input only for marking it as accepted)

How to install or get access to sqlite3 from adb shell

I need a way to install or somehow get access to sqlite3 in the adb shell. I have rooted my device.
I've tried to find an answer but the closed I could come is:
Why do I get a "sqlite3: not found" error on a rooted Nexus One when I try to open a database using the adb shell?
But I don't think it's good idea to push my windows sqlite3.exe on a linux system?
So is it possible to install the sqlite3 terminal browser somehow?
[SOLUTION]
From the different comments and some asking around at #android-dev (irc), I found a solution. First I copied the database file to my desktop. But fist I had to install BusyBox, because cp isn't included?!? After that ran I into the problem that I couldn't pull or push from anywhere but /sdcard/ . I could then use /sdcard/ as a "middle station" and pull/push my db.
Then I got exhausted! I really had to have my sqlite terminal explore. Then I got the idea to start the emulator pull the sqlite binary from /system/xbin/sqlite3. Then remount /system with rw:
# mount -o remount,rw -t yaffs2 /dev/block/mtdblock3 /system
and push sqlite to the /sdcard/, and from there copy it to /system/xbin/
Now it works :D
Download this app from google play will help you install sqlite3 on android https://play.google.com/store/apps/details?id=ptSoft.util.sqlite3forroot
You don't need root to pull the database from your device. Simply run the following commands:
adb shell run-as <package-name> "cp databases/<db_name>.db /sdcard/ && exit"
adb pull /sdcard/<db_name>.db ~/Downloads/
From there, you can use sqlite3 for whatever operating system you're using (http://www.sqlite.org/download.html), or a sqlite browser such as "DB Browser for SQLite" (http://sqlitebrowser.org/)
I use Rajath's technique... Adb "Pull" the db from the emulator/device, work on it, then adb "push" it back onto/into the emulator device.
also:
I use the free SQLite Editor from the Android Market. I have not rooted my LG Ally and therefor can only edit database tables on my sdcard with SQLite Editor.
Rajath suggests using the adb to push and pull the databases to and from the emulator/device. The work on the database with the windows (or whatever) sqlite3 program you have. He does not suggest pusing the windows sqlite3 onto the Android device, IMHO.
I note that java/android "query()" sends actual SQL commands programmacitacly to ones program with user input. I conclude that sqlite3 is in Android somewhere.
When using the emulator Dev Tools is available, and way down at the bottom of the list is the Terminal Emulator. This allows exploration of file structure of Android in the emulator. However using "adb shell" from the PC has root permissions.
good luck. cactus mitch
You can do this with adb shell without issue.
In terminal or CMD (assuming you have the ADB path set and your phone has ROOT) type:
$ adb shell
$ cd data/data/com.nameofyourpackage/databases/
$ ls to find the name of your database
$ sqlite3 nameofyourdb.db
Then you can use .tables .schema to see the data you need to create the appropriate query.

Android ADB commands missing

I've got Android 2.2 on my phone and I'm doing some DB development. I've been trying to query my DB using sqlite3, but it looks like sqlite3 is missing from my phone. So then I tried a few other commands (su, find etc) and they ALL seem to be missing.
Can anyone tell me where they have gone please? I read somewhere that sqlite3 at least might have been accidentally left out of 2.2, but surely they can't all have been missed off can they?
Thanks
With the new release, adb has moved to platform-tools while all other tools are still under tools folder.
You can either copy them or add them to the Windows Path (if you are using windows).
Try:
$ adb -s <serialno> shell type sqlite3
sqlite3 is /system/xbin/sqlite3
to determine if sqlite3 is available or not on you device.
The command you entered in one of your comments is malformed, it should be (after you determined that sqlite3 is available):
$ adb -s <serialno> shell sqlite3 /data/data/com.example.google.rss.rssexample/databases/rssitems.db
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
Those aren't adb commands those are binaries usually installed on rooted devices. If your device isn't rooted you aren't going to be able to install them either.
However, what you can do to get into your apps database. Use the activity manager command set-debug-app [options] <PACKAGE> This will let you access your apps database without being rooted, then you can pull it to your desktop and use sqlite3 from there.
Something like this:
adb shell
am set-debug-app com.example.appname
exit ##exit adb shell
adb pull data/data/com.example.appname/databases/exampledb.db exampledb.db
sqlite3 exampledb.db
Your app needs to be set to debugging true in the manifest for this to work.

Categories

Resources