SQLite is an open-source server-less database engine. SQLite supports transacations and has no configuration required. SQLite adds powerful data storage to mobile and embedded apps.

Creating and connecting to a database

Import android.database.sqlite.SQLiteDatabase into the application.

Use openOrCreateDatabase() method to create or connect to a database.

Sample code pasted below,

package example.SQLiteTest;

import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;

public class SQLiteTest extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SQLiteDatabase db;
db = openOrCreateDatabase(
"TestData.db"
, SQLiteDatabase.CREATE_IF_NECESSARY
, null
);
}
}

Run the above code snippet, to create a database called TestData.

Android stores the above database in,

/data/data/[application package name]/databases.

Emulator’s status and port can be found out by giving the following command,

C:\[..path..]\android-sdk-windows\tools>adb devices

List of devices attached
emulator-5554 device

To view the data base created above, we need to log in to the adb shell. (I’m using windows),

In the command prompt navigate to your android-sdk folder,

C:\[..path..]\android-sdk-windows\tools>adb -s emulator-5554 shell

# ls /data/data/example.SQLiteTest/databases
TestData.db

# sqlite3 /data/data/example.SQLiteTest/databases/TestData.db

SQLite version 3.5.9

Enter “.help” for instructions

sqlite> .tables

android_metadata

sqlite>

Creating Tables

execSQL() statement is used to execute queries.

Before creating tables, certain database properties should be set using the following methods,

setVersion(), setLocale(), and setLockingEnabled().

Append this code to the previous example for creating tables (tbl_countries and tbl_states)..

db.setVersion(1);

db.setLocale(Locale.getDefault());

db.setLockingEnabled(true);

final String CREATE_TABLE_COUNTRIES =

"CREATE TABLE tbl_countries ("

+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"

+ "country_name TEXT);";

final String CREATE_TABLE_STATES =

"CREATE TABLE tbl_states ("

+ "id INTEGER PRIMARY KEY AUTOINCREMENT,"

+ "state_name TEXT);";

db.execSQL(CREATE_TABLE_COUNTRIES);

db.execSQL(CREATE_TABLE_STATES);

Now in shell if you execute the following command you should see these two tables that we’ve created.

sqlite> .tables

.tables

android_metadata tbl_countries tbl_states

sqlite>

Inserting Records

import android.content.ContentValues;

Use ContentValues instance to create a series of table field to data matchings that will be passed into an insert() method.

Code snipped pasted below..

ContentValues values = new ContentValues();

values.put("country_name", "India");

db.insert("tbl_countries", null, values);

sqlite> select * from tbl_countries;

select * from tbl_countries;

1|India

Incoming search terms:

  • yii sqlite ?cannot be found in the database
  • sql command make a table sqlite android
  • how to create database tables in android
  • CreatingSQLitedatabase&TablesinAndroid|SEO SEM Web eCommerce iOS Cloud HTML5 ChromeAppsandAndroid
  • android multiple table database example
  • android create table
  • android developer sqlite database display table
  • how to pre create value in sqlite database android
  • jquery mobile how to add new column sqlite table in device
  • jquery mobile where is location sqlite database create

Filed under: Android

Like this post? Subscribe to my RSS feed and get loads more!