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
Related
When using sqlite3 through adb shell arrow keys, instead of moving the cursor to the desired position or summoning the history facility, the following is showed in the screen: ^[[A, ^[[B, ^[[C, ^[[D.
I'm using Mac OS X and I have tried Terminal and iTerm terminal emulators.
Does anybody know how to fix this?
To allow editing and history in the input of a console program, that program must be linked with the readline library.
The sqlite3 tool does support readline, but on Android, readline support has been disabled.
(Probably because readline is licensed only under the GPL.)
A workaround would be to use a local version of SQLite with readline support.
Copy a database file from your device to your local machine:
adb pull <database-file-on-device>
Use your local version of SQLite to access the database file:
sqlite3 <database-file-on-local>
If you made changes you can transfer them to the device. Copy your local database file from your local machine to your device:
sqlite3 <database-file-on-local> <database-file-on-device>
You can use the previous command functionality in the adb shell. So just adb shell. Then cd to the /data/data//databases directory. From here run (for example): sqlite3 "select * from "
Then you can use up arrow to redo that command. Kind of a hack, but way better than having to retype the command inside the sqlite3 interactive prompt.
Working off of Khanad's answer: I wrote a shell script on the device and I send my sqlite commands through it.
First create a script file on the device and make it executable:
# get on the device
$ adb shell
# get write access on the device (this will also put you in the data/data directory)
$ run-as com.YOUR_PACKAGE.YOUR_APP_NAME
# create the script file
$ touch qlite
# make it executable
$ chmod +x qlite
Then add this code to the script. Remember to put your actual app name in there. (The echoes just add a little breathing room to the output.)
echo
sqlite3 databases/YOUR_APP_NAME -cmd ".mode column" ".headers on" "$1;"
echo
Then you just have to do something like:
./qlite "select * from table limit 1"
The script will pass the sqlite command through, tack on the semi-colon, pretty print it with column names and you can use the up arrow to get your last command. And because you have write access you can make updates directly to the DB on the device.
I hope this makes someone's life easier!
Use ADB shell instead of sqlite3 shell to get the advantage of the readline package.
generic_x86:/data/data/com.example.musicplayer/databases $ sqlite3 music_player_database -line 'select * from lastplayedsong;'
Just make sure you are in the correct package and don't put .db at the end of the database name otherwise a new database would be created and the query would return you no result.
P.S: source
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
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.
Is there way to add, edit or delete records manually in sqlite database in Android Eclipse emulator?
Thanks
Open shell to your emulator device using adb :
adb -s emulator-5554 shell
then you can use the sqlite3 to get into sqlite shell :
#sqlite3
sqlite3
SQLite version 3.5.9
Enter ".help" for instructions
sqlite>
You can execute SQLs here. See if that helps.
You can also open the database as below by going to the directory where you have your database:
#sqlite3 mydatabase.db
I've read the answer to a question as to how to access the contents of the databases, however I cannot seem to get it to work on my machine. Here is the shell log:
C:\android-sdk-windows\tools>adb -s emulator-5554 shell
# sqlite3 /data/data/com.android.demo.notepad2/databases/notes
sqlite3 /data/data/com.android.demo.notepad2/databases/notes
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> .tables
.tables
sqlite> ^C
C:\android-sdk-windows\tools>
SQLite simply echos my commands back to me, even although the Eclipse file browser tells me it exists. If I use the sqlite3 tool and use ".tables" the commands are accepted.
Is the SQLite syntax different through the emulator is am I missing something?
(Sorry for so many questions, there doesn't seem to be much coherent documentation on Android!)
Thanks!
I can tell you that it works fine for me on Android 2.0.1:
$ adb shell
# cd /data/data/apt.tutorial
# ls
lib
databases
shared_prefs
# cd databases
# ls
lunchlist.db
# sqlite3 lunchlist.db
SQLite version 3.5.9
Enter ".help" for instructions
sqlite> .tables
android_metadata restaurants
sqlite> .exit
# exit
You can always download the database file using DDMS or adb pull and use a desktop SQLite client to examine it. For example, I use the SQLite Manager plugin for Firefox.
Also, bear in mind that SQLite has no default file extension, so if your database is not notes but notes.db or notes.sqlite or something, you'll need to specify the extension.
Also also, I have not tried this on Windows, and there's a possibility that there is something goofy with the Windows command prompt and the limited shell available on Android devices that is causing your difficulty.
If you want to issue sqlite3 statements from command line use something like
$ adb -e shell sqlite3 -batch /data/data/com.example.dbsample/databases/db '.tables'
android_metadata
$ adb -e shell sqlite3 -batch /data/data/com.example.dbsample/databases/db 'select * from android_metadata;'
en_US
the obvious advantages are that you can rely on your shell history and you can use this in scripts.
I had the same problem and discovered that contrary to the documentation you shouldn't put the ".db" extension on the database file name.
Note: I'm using win7 and 2.1 emulator.
I had the same issue ,spent almost an hour ,posting here so that it will save someone else time ,check if you have given the correct filename along with the extension, there are two files under the databases folder myDB.db and myDB.db-journal and when i ran
"sqlite3 /data/data/com.my.package/databases/myDB"
and
.tables listed nothing, sqlite3 created a new db with the name myDB , it did not open myDB.db