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.
Related
I'd like to insert data directly into the sqlite database of my app but I cannot find it anywhere on android studio path, even on my root path:
$sudo find / -type f -name 'myapp.db'
I know several similar questions have been asked before but the answers for Windows did not help me on Ubuntu Linux. So appreciate your help.
Android Studio does not store the database locally in your computer. The databases only exist in the devices & every time you deploy to a new device, your database will be created new in that new device. That is why you can't find it in your computer. Here is where the database is located in the device:
/data/data/full_qualified_java_package_name/databases/database_name.db
Now if you would like to insert data directly, you can use the terminal in Android Studio & use ADB to pull the database off the emulator, modify it, and push it back in. Heck I am sure that if you know enough Linux you could probably insert what you need into it without pulling it from the device. Here are some sample commands for the Android Studio terminal for that:
~/Android/Sdk/platform-tools/adb devices
Get the device number, then:
~/Android/Sdk/platform-tools/adb -s emulator-#### pull /data/data/full_qualified_java_package_name/databases/database_name.db <local-filepath>
And to send it back in, it is just:
~/Android/Sdk/platform-tools/adb -s emulator-#### push <local-filepath> /data/data/full_qualified_java_package_name/databases/database_name.db
Example:
~/Android/Sdk/platform-tools/adb -s emulator-5554 pull /data/data/com.danielkaparunakis.stackoverflowquestions/databases/Questiondatabase.db /Users/DanielKaparunakis/Desktop
Additional tip: If you leave the blank when you pull like this:
~/Android/Sdk/platform-tools/adb -s emulator-5554 pull /data/data/com.danielkaparunakis.stackoverflowquestions/databases/Questiondatabase.db
It will automatically pull it to your project's root folder.
It will save it in the internal storage of every device, if you don't have a rooted device it will not allow you to pull it, but, if you are using an emulator you will be able to pull it.
https://developer.android.com/training/basics/data-storage/databases.html
You app's db is only on the device. You can pull it from any connected device – non-rooted physical devices as well. This script pulls it from the first device.
This trick is run-as <package name> which runs a shell the app's directory with full access to the app's data.
Replace $package with your app's package name and replace $db with the name of you app's db.
$ LC_ALL=C adb exec-out run-as $package cat databases/$db >db.sqlite
LC_ALL=C is to avoid some strange locale behavior on some systems.
adb is by default installed by Android Studio to ~/Android/Sdk/platform-tools/adb.
Update
The program 'adb' is currently not installed. To run 'adb' please ask your administrator to install the package 'android-tools-adb'
This is Ubuntu telling you that you can install it from the Ubuntu package manager.
Normally you would already have it as a part of Android Studio.
Update 2
I don't have a script yet for pushing it back since push and run-as don't work together. You would have to do something like this (untested).
$ adb push db.sqlite /sdcard/temp.sqlite
$ cat <<EOF | adb shell
run-as $package
cat /sdcard/temp.sqlite >databases/$db
exit
exit
EOF
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 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)
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.
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