Android – Using SQLite DataBase


Androidsqlite


  • Android OS comes with SQLite Database already built-in. This article will explain how to store and draw data.
  • Currently I am working with Android version 2.2.


1. Extend SQLiteOpenHelper


SQLiteOpenHelper is the main class responsible for SQLite operations. You will need to extend it.

public abstract class DBManager extends SQLiteOpenHelper{
	
	private static final String DB_FILE_NAME = "test.db";
	private String tableName;
	private String[] columns;
	
	public DBManager(Context context,String tableName, String [] columns) {
		super(context, DB_FILE_NAME, null, 1);
		this.tableName = tableName;
		this.columns = columns;
	}
.
.
.

After extending SQLiteOpenHelper, you will need to trigger the super constructor with the proper parameters – the context (which Activity inherit from it) , the DB file name, CursorFactory (which we wont discuss in this article, leave it as null for now) and the DB version.

2. Create a table

One of the functions you have inherited from SQLiteOpenHelper is public void onCreate(SQLiteDatabase db), which is triggered when the DB was created. You can use this function to create the table.

public abstract class DBManager extends SQLiteOpenHelper{
	
	private static final String DB_FILE_NAME = "test.db";
	private String tableName;
	private String[] columns;
	
	public DBManager(Context context,String tableName, String [] columns) {
		super(context, DB_FILE_NAME, null, 1);
		this.tableName = tableName;
		this.columns = columns;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + tableName + 
		" (" + BaseColumns._ID + " integer primary key autoincrement "; 
		
		for (int i=0;i<columns.length; i++) {
			sql+=", "+columns[i];
		}
		
		sql+=" ) ";
		db.execSQL( sql );
	}
.
.
.

3. Create SQL queries

  • From this point it is fairly easy to create SQL queries.
  • Your object is now holding reference to 2 objects one for writing/deleting and one for reading. You will use them to make operations.
package com.aviyehuda.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
import android.util.Log;

public abstract class DBManager extends SQLiteOpenHelper{
	
	private static final String DB_FILE_NAME = "test.db";
	private String tableName;
	private String[] columns;
	
	public DBManager(Context context,String tableName, String [] columns) {
		super(context, DB_FILE_NAME, null, 1);
		this.tableName = tableName;
		this.columns = columns;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		String sql = "create table " + tableName + 
		" (" + BaseColumns._ID + " integer primary key autoincrement "; 
		
		for (int i=0;i<columns.length; i++) {
			sql+=", "+columns[i];
		}
		
		sql+=" ) ";
		
		db.execSQL( sql );
		
	}

	public void insert(ContentValues values){
		getWritableDatabase().insert(tableName, null, values);
	}
	
	public Cursor getAll(){
		return getReadableDatabase().query(tableName,
							 null, null, null, null, null, null);
	}
	
}


Further reading – Android developer website


zip Download demo project

2 thoughts on “Android – Using SQLite DataBase

  1. thank u for the nice sqlite database code. please help me remaining functionalities in sqlitedtabse clearly

    Thanks,
    Laxman

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>