I am developing an automation on android platform and got stuck at the db part of the app.
I need to clear all the data from a table "tablename" through adb commands. I have got the following commands that do the job manually but I cannot automate it.
adb shell sqlite /data/data/.../databases/log.db
delete * from tablename
When I create a batch file for this the
sqlite>
had opened but the delete command was not executed. How can I achieve this?
batch file data:
adb shell sqlite3 /data/data/.../databases/activitylog.db
delete * from events;
The SQL command to execute must be a single parameter, so you have to quote it:
adb shell sqlite3 .../activitylog.db 'delete from events'
And I took the liberty of correcting the SQL syntax.
Your Device needs to be rooted for this.
Way one:
Run your sqlite3 command after "su".
I prefer the following way:
adb shell
su
cd <toYourDbDir>
sqlite3 /data/data/com.symantec.mobilesecurity/databases/activitylog.db delete * from events
If you have to do this again, just use the up-arrow to get the last command and execute it with enter.
Way two:
Install sqlite Database Editor, open the Db and edit what you want.
Related
i have created a database using SQliteCursor in android. but non of my queries work and it always give me no such table exception.so i want to see the database to check if it is even created?!
so here is my question:how can i see my created database with code as the program is running???
I saw so many answers but they all assume there is a database in DDMS perspective.
is there any tool I can use? what can I do?
besides I am using cursor to enter queries.
thanks.
You can pull the database from the device to your local disk by executing following commands:
First make your database file accessible by running chmod using run-as command of adb
adb shell "run-as package.name chmod 666 /data/data/package.name/databases/file"
Then try to pull that file in your local disk
adb pull /data/data/package.name/databases/file
Just make sure that you have marked the application to debuggable=true in the manifest.
To view this database you can download a mozilla plugin i.e SqliteManager and can view the full schema of DB. I always use this and it is really good.
I made a executable file for this process please add below lines in a file with .exe or sh extension depending on your system OS:
adb shell "run-as yourpackagename chmod 666 databases/yourdbname.db"
adb shell "cp /data/data/yourpackagename/databases/yourdbname.db /sdcard/"
adb shell "run-as yourpackagename chmod 600 databases/yourdbname.db"
adb pull /sdcard/yourdbname.db
Replace yourdbname with your Db name and yourpackagename with the package name of your app.
Please post if you got stuck somewhere in the steps.
Please paste some code for better understanding.
or try this
Go to DDMS => data => data => here you have to find your project like com.example.example. In this you will easily get database file and you can save it or check whether it is created or not.
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 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
I am using sqlite in my application.I Check Eclipse DDMS the database is created.How to check wheather the table is created or not in command prompt.pls tell shell command of open database and open table in command prompt..........
cd to your data base directory
$ sqlite3 yourdb.db
.schema;
or
select * from yourtable;
use '.quit ' back to command prompt
'.help' also help you
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