I've begun developing on the android platform and I thought I'd write a series of articles on things I've learned since I started. Today's is going to be on command line access to the embedded sqlite database. Access to the database can be obtained using the adb tool (which is a pretty amazing tool if you haven't tried it). The adb tool lets you interact with an emulator while it's running. Two of the uses I'll be talking about are its ability to navigating the internal file system through the shell and transferring files between your local file system and the emulator file system.
To use the adb tool, add the android SDK tools directory to your path. Once the tools directory is part of the path open a command prompt. On the command prompt open the shell by executing:
adb shell
For this command to be successful the emulator must be running. Adb attaches itself to the emulator and provides command line access within the emulator. Once attached, navigate to your application's directory by typing
cd data/data/<application package>
This directory is where your application stores it's data.
From here enter the databases directory
cd databases
Any databases created by your application will be present in this directory. The databases directory is only created once your application has run within the emulator and created the database. If you have not run your app or if it doesn't interact with the database then this directory will not exist. Once in the database directory, you should see a database file named by your application. This is your application's database, now lets begin interacting with it!
Command-line access to the database is provided by sqlite3. On the command line try executing
sqlite3 myapp.db
This will open the database and allow any typical sql commands to be executed. To see a listing of sqlite commands, type '.help' which will display a usage of all commands. A couple of the more useful commands to begin with are .tables to list all tables in the database and '.schema table' to display the schema used to create the table.
If you prefer one of the many sqlite graphical interfaces instead of the command line access. The following describes steps to extract the sqlite database from the emulator to use on your PC with your favorite tools. The adb tool can push and pull files between your local file system and the one on the emulator. To pull the database out of the emulator, the adb tool provides a pull command with the following syntax
adb pull <remote> <local>
For example: adb pull data/data/<application package>/databases/app.db local_file.db
Once the database file is pulled from the emulator it can be opened with any of your favorite sqlite tools.
Comments
No comments have been added
PREVIEW