SQL Zone is brought to you in partnership with:

Avi is a senior developer with a passion for Java technologies… http://www.aviyehuda.com/ Avi is a DZone MVB and is not an employee of DZone and has posted 25 posts at DZone. You can read more from them at their website. View Full User Profile

Android – Using SQLite DataBase

12.01.2010
| 25536 views |
  • submit to reddit
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

References
Published at DZone with permission of Avi Yehuda, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)