Saturday, June 20, 2009

Tutorial : How to access a Android database by using a command line.

When your start dealing with a database in your program, it is really important and useful to be able to access it directly, outside your program, to check what the program has just done, and to debug.

And it is important also on Android.

Here is how to do that :

1) Launch the emulator (or connect your real device to your PC ). I usually launch one of my program from Eclipse for this.
2) Launch a command prompt in the android tools directory.
3) type adb shell.
This will launch an unix shell on your emulator / connected device.
4) go to the directory where your database is :
cd data/data
here you have the list of all the applications on your device
Go in your application directory ( beware, Unix is case sensitive !! )
cd com.alocaly.LetterGame
and descend in your databases directory :
cd databases
Here you can find all your databases. In my case, there is only one ( now ) : SCORE_DB
5) Launch sqlite on the database you want to check / change :
sqlite3 SCORE_DB
From here, you can check what tables are present :
.tables
6) enter any SQL instruction you want :
select * from Score;

This is quite simple, but every time I need it, I don't know where to find it.

22 comments:

Unknown said...

Great post, thanks for the walkthrough!

Ashish said...

Really Nice and Step by Step article for connecting to android databases.

thanks for the post.

Caféïne said...

Thanks a lot.
Really Nice.

Anonymous said...

I am getting permission denied error, can you help?

Anonymous said...

Thanks I really needed this!

Android said...
This comment has been removed by a blog administrator.
android logiciel said...

Thank you,

nice article for connecting to android databases.

Anonymous said...

thanks it sounds useful.
But can you pls tell me how to take the android tools directory? is it a windows folder?

AndroidBlogger said...

The tools directory is the tool in the sdk where the Android tools are located ( so, yes, it is a Windows folder ).
In recent SDK, it is located there :
/platform-tools/

Alex Lewin said...

I got permission denied, too:

$ sqlite3
sqlite3: permission denied
$ cd databases
$ ls
opendir failed, Permission denied
$ sudo -s
sudo: permission denied
$ su -
su: permission denied
$

Any suggestions?

Anonymous said...

answer to the permission denied is i don't think your typing cd

Because if you don't it says that

Wils said...

Nice man!
Pls, do u know how to do suing Nexus S device?
Do I really need to root?

Anonymous said...

try adb root if u get permission denied

neha said...

Brilliant Post! Very well written..
Neha

Anonymous said...

Thank you very much. It's quite helpful

Raphael Nunes said...

If cd doesn't work use ls just to list directories. This way you will know where is db. Then use
>> sqllite3 [here your directories you found use ls command]

sqllite3 data/data/r.nunes.package/databases/Apication.db

Anonymous said...

Thanks a lot!

Nhu said...

I'm the same. That's why I'm beginning to collect tutorials/post that I find useful. Thanks.

Unknown said...
This comment has been removed by the author.
Deepa said...

very resourceful..I needed it.thanq so much for sharing..

uri said...

Doesn't work. Get a permission denied error when doing LS to list the files.
Getting a command not found error when typing "sqlite3". Running Android Ice Cream sandwich

Rogério Schneider said...

How to build the sqlite3 binary and library yourself.

I have put together some build scripts to compile SQLite for Android Native Code using the Android NDK. It builds the SQLite CLI in two versions: Statically and Dynamically Linked, as well as it's Static and Shared Libraries. You may get the scripts from my GitHub and build the binaries yourself:

https://github.com/stockrt/sqlite3-android

Hope this will be useful for someone.