SQLite is available on the Android device itself. The executable is in the /system/bin directory of the device. You can see that this directory contains the shell commands like ls, ps, etc., as well as sqlite3, dalvikvm, and dexdump utilities.

Code Listing 1. Contents of system/bin

#pwd

pwd

/system/bin

# ls -l

ls-l

-rwxr-xr-xrootroot1962008-02-2901:09am

-rwxr-xr-xrootroot23422008-02-2901:09dumpstate

-rwxr-xr-xrootroot2082008-02-2901:09input

-rwxr-xr-xrootroot2122008-02-2901:09monkey

-rwxr-xr-x root root 196 2008-02-29 01:09 pm

…

lrwxr-xr-x root root 2008-02-29 01:16 mount -> toolbox

lrwxr-xr-xrootroot2008-02-2901:16notify->toolbox

-rwxr-xr-xrootroot280322008-02-2901:16dexdump

-rwxr-xr-xrootroot71002008-02-2901:16dumpsys

-rwxr-xr-xrootroot79042008-02-2901:16mem_profiler

-rwxr-xr-xrootroot64802008-02-2901:16service

-rwxr-xr-xrootroot43202008-02-2901:16rild

-rwxr-xr-xrootroot49282008-02-2901:16sdutil

-rwxr-xr-xrootroot264882008-02-2901:16sqlite3

-rwxr-xr-xrootroot41482008-02-2901:16dexopt

-rwxr-xr-xrootroot49082008-02-2901:16dalvikvm

-rwsr-sr-xrootroot32002008-02-2901:16surfaceflinger

-rwxr-xr-xrootroot54202008-02-2901:16app_process

-rwxr-xr-xrootroot394082008-02-2901:16runtime

-rwxr-xr-xrootroot29202008-02-2901:16system_server

-rwxr-xr-x root root 9168 2008-02-29 01:17 pv

Where are the SQLite databases on an Android Device?

By default, the SQLite databases have an extension .db. For example, a NotePad application may use note_pad.db database. This might be created from code using Android SDK’s SQLite classes/methods. By default, the SQLite database for an application is in the /data/data/<app>/databases directory. As you can see from the listing below, the notepad database is in the /data/data/com.google.android.notepad/databases directory.

Code Listing 2. The location of the notepad database

#pwd

pwd

/data/data/com.google.android.notepad/databases

# ls -l

ls-l

-rw-rw---- app_8 app_8 3072 2008-03-18 18:23 note_pad.db

You can see a whole bunch of these sub-directories under the /data/data directory.

Code Listing 3. The /data/data directory

#pwd

pwd

/data/data

# ls

ls

com.google.android.notepad

com.google.android.samples

com.google.android.gtalkservice

com.google.android.phone

com.google.android.maps

com.google.android.providers.im

com.google.android.home

com.google.android.googleapps

com.google.android.gtalksettings

com.google.android.fallback

com.google.android.development

com.google.android.contacts

com.google.android.browser

com.google.android.providers.telephony

com.google.android.providers.settings

com.google.android.providers.media

com.google.android.masfproxyservice

com.google.android.providers.googleapps

com.google.android.providers.contacts

android

Starting the SQLite command line program

Starting generically

Typing sqlite3 in the Android Shell will open the SQLite command line program (since sqlite3 is in the /system/bin directory). Typing .help gives the summarized set of commands available from here.

Code Listing 4. The SQLite commands available from the command line utility

#sqlite3

sqlite3

SQLiteversion3.5.0

Enter ".help" for instructions

sqlite> .help

.help

.bailON|OFFStopafterhittinganerror.DefaultOFF

.databasesListnamesandfilesofattacheddatabases

.dump?TABLE?...DumpthedatabaseinanSQLtextformat

.echoON|OFFTurncommandechoonoroff

.exitExitthisprogram

.explainON|OFFTurnoutputmodesuitableforEXPLAINonoroff.

.header(s)ON|OFFTurndisplayofheadersonoroff

.helpShowthismessage

.importFILETABLEImportdatafromFILEintoTABLE

.indicesTABLEShownamesofallindicesonTABLE

.loadFILE?ENTRY?Loadanextensionlibrary

.modeMODE?TABLE?SetoutputmodewhereMODEisoneof:

csvComma-separatedvalues

columnLeft-alignedcolumns.(See.width)

htmlHTML<table>code

insertSQLinsertstatementsforTABLE

lineOnevalueperline

listValuesdelimitedby.separatorstring

tabsTab-separatedvalues

tclTCLlistelements

.nullvalueSTRINGPrintSTRINGinplaceofNULLvalues

.outputFILENAMESendoutputtoFILENAME

.outputstdoutSendoutputtothescreen

.promptMAINCONTINUEReplacethestandardprompts

.quitExitthisprogram

.readFILENAMEExecuteSQLinFILENAME

.schema?TABLE?ShowtheCREATEstatements

.separatorSTRINGChangeseparatorusedbyoutputmodeand.import

.showShowthecurrentvaluesforvarioussettings

.tables?PATTERN?ListnamesoftablesmatchingaLIKEpattern

.timeoutMSTryopeninglockedtablesforMSmilliseconds

.widthNUMNUM...Setcolumnwidthsfor"column"mode

sqlite>

Opening a database

You would, of course, want to see the data and structure of a particular database. In the listing below, the note_pad.db is opened for further querying. Here, I went to the directory where the database is; however, you can also specify the full path.

Code Listing 5. Opening a specific database from SQLite command line

#pwd

pwd

/data/data/com.google.android.notepad/databases

# ls

ls

note_pad.db

# sqlite3 note_pad.db

sqlite3note_pad.db

SQLiteversion3.5.0

Enter".help"forinstructions

sqlite>

Example Commands from the SQLite command line program

Getting the list of tables

You can get the list of all the tables in the current database in two ways: from the .tables command or by querying the sqlite_master table.

Code Listing 6. List of tables

sqlite>.tables

.tables

android_metadata notes

sqlite> select * from sqlite_master;

select*fromsqlite_master;

table|android_metadata|android_metadata|2|CREATETABLEandroid_metadata(locale

TEXT)

table|notes|notes|3|CREATETABLEnotes(_idINTEGERPRIMARYKEY,titleTEXT,note

TEXT,created INTEGER,modified INTEGER)

Getting the schema of the tables

By using the .schema command, you can get the schema of either all of the tables or a single table.

Code Listing 7. Schema of tables

sqlite>.schema

.schema

CREATETABLEandroid_metadata(localeTEXT);

CREATETABLEnotes(_idINTEGERPRIMARYKEY,titleTEXT,noteTEXT,createdINTEGER

,modified INTEGER);

sqlite> .schema notes

.schemanotes

CREATETABLEnotes(_idINTEGERPRIMARYKEY,titleTEXT,noteTEXT,createdINTEGER

,modified INTEGER);

SQL Queries

You can issue queries like ‘select * from notes’ to get the data from the notes table. As you can see from the figure below, I have entered a series of notes in the notepad application.

Figure 1. Notes in the Note pad application

After entering the notes, if you query the notes table, you will get the data in the _id, title, note, created, and modified columns.

Code Listing 8. Data from notes

sqlite>.schemanotes

.schemanotes

CREATETABLEnotes(_idINTEGERPRIMARYKEY,titleTEXT,noteTEXT,createdINTEGER

,modified INTEGER);

sqlite> select * from notes;

select*fromnotes;

1|TitleofNote

Thisisatestnote.|Thisisatestnote.|1205871448827|1205873944838

3|Anothernote

Sometext.|Athirdnote

Sometext.|1205873990394|1205874145996

4|Onemore

Bodyhere|Onemore

Body here|1205874074992|1205874088072

And, finally, we can take a look at how this table was created.

Code Listing 9. Creation of the notes table

public class NotePadProvider extends ContentProvider {

…

    private static class DatabaseHelper extends SQLiteOpenHelper {

        @Override

publicvoidonCreate(SQLiteDatabasedb){

db.execSQL("CREATETABLEnotes(_idINTEGERPRIMARYKEY,"

+"titleTEXT,"+"noteTEXT,"+"createdINTEGER,"

+"modifiedINTEGER"+");");

}

…

    }

…

    @Override

publicbooleanonCreate(){

DatabaseHelperdbHelper=newDatabaseHelper();

mDB=dbHelper.openDatabase(getContext(),DATABASE_NAME,null,DATABASE_VERSION);

return(mDB==null)?false:true;

    }

…

}

As you can see above, CREATE TABLE sql has been used to create the notes table. Classes like SQLiteOpenHelper and ContentProvider from Android SDK are put to use here.