Tuesday, August 6, 2013

Running (Android) SQLite Database from Command Prompt

How to access a Android SQLite Database by using a command line


You can access SQLite database from the command prompt. It is important part of the program to check whether appropriate database has been created in your program or not and to check what program has just done and to debug.

Steps to connect/access a Android Database by using a command line:

1. Launch the emulator: You can use any android device connected to your PC or usually you can launch emulator from eclipse for this.

2. Launch a command prompt. Go to Windows Start Button -> Type CMD on Search/Run Window

3. Change current directory to your android tools directory
i.e. In my case directory path will be cd "C:\Users\Rushabh\Desktop\Android\android-sdk-r22.0-1-windows"

4. Type adb shell - this will launch an unix shell on your emulator/connected device.

C:\Users\client>cd "c:\Users\client\Desktop\Andriod\android-sdk_r22.0.1-windows"c:\Users\client\Desktop\Andriod\android-sdk_r22.0.1-windows>adb shellerror: device not found (emulator not found)c:\Users\client\Desktop\Andriod\android-sdk_r22.0.1-windows>adb shellerror: device offline (emulator not found)
c:\Users\client\Desktop\Andriod\android-sdk_r22.0.1-windows>adb shellroot@android:/ # 
(emulator is active)


5. Go to directory where your database is stored:
i.e. In my case database path is cd "data/data/com.sampleprogram.program/databases"

c:\Users\client\Desktop\Andriod\android-sdk_r22.0.1-windows>adb shell
root@android:/ # cd data/data/com.sampleprogram.program/databas
cd data/data/com.sampleprogram.program/databases

root@android:/data/data/com.sampleprogram.program/databases #

6. To launch SQLite Database type sqlite3 SampleDB.db 
From here you entered into your sqlite database and you can perform any T-SQL operation you want

root@android:/data/data/com.sampleprogram.program/databases # sqlite3 SampleDB.db
e3 SampleDB.db                                                          <
SQLite version 3.7.11 2012-03-20 11:35:50
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

7. Type .schema: it shows create table signature of any table created

sqlite> .schema
.schema
CREATE TABLE EMPLOYEE (EMP_ID INTEGER PRIMARY KEY AUTOINCREMENT, USER_ID TEXT NOT NULL, PASSWORD TEXT NOT NULL, FIRST_NAME TEXT NOT NULL, LAST_NAME TEXT NOT NULL, EMAIL TEXT NOT NULL, JOB_CODE TEXT NOT NULL, FOREIGN KEY (JOB_CODE) REFERENCE S EMP_JOBROLLES (JOB_CODE));

8. Type .help and it will list all command supported by sqlite

IMP: It is required to write proper name of the database otherwise sqlite will create a new database and you'll get confused about what's going on. 

1 comment:

  1. 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.

    ReplyDelete