Skip to content

aviyehuda.com

Menu
  • Open Source
  • Android
  • Java
  • Others
  • Contact Me
  • About Me
Menu

Android – Using SQLite DataBase

Posted on 21/10/2010


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. Apichit says:
    29/03/2011 at 10:11

    Soso, If I want create one databases but have many Table I don’t idea I want example, Thank you!.

    Reply
  2. laxman says:
    05/05/2012 at 08:39

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

    Thanks,
    Laxman

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


About Me

REFCARD – Code Gems for Android Developers

Categories

  • Android
  • AWS
  • AWS EMR
  • bluetooth
  • Chrome extension
  • ClientSide
  • Clover
  • Coding Coventions
  • Data Lake
  • General
  • GreaseMonkey
  • Hacks
  • hibernate
  • hibernate validator
  • HTML5
  • HtmlUnit
  • Image Manipulation
  • Java
  • Java Technologies
  • JavaScript
  • Java_Mail
  • JEE/Network
  • Job searching
  • Open Source
  • Pivot
  • projects
  • Pure Java
  • software
  • Spark
  • Trivia
  • Web development

Archives

  • March 2022 (1)
  • January 2022 (1)
  • January 2021 (1)
  • December 2018 (1)
  • August 2018 (1)
  • October 2013 (1)
  • March 2013 (1)
  • January 2013 (2)
  • July 2012 (1)
  • April 2012 (1)
  • March 2012 (1)
  • December 2011 (1)
  • July 2011 (1)
  • June 2011 (1)
  • May 2011 (2)
  • January 2011 (1)
  • December 2010 (1)
  • November 2010 (3)
  • October 2010 (4)
  • July 2010 (1)
  • April 2010 (2)
  • March 2010 (1)
  • February 2010 (2)
  • January 2010 (5)
  • December 2009 (10)
  • September 2009 (1)
 RSS Feed
1d96f52e7159fe09c7a3dd2a9816d166-332
©2023 aviyehuda.com | Design: Newspaperly WordPress Theme