Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Android – Using SQLite DataBase

DZone's Guide to

Android – Using SQLite DataBase

· Mobile Zone
Free Resource

Download this comprehensive Mobile Testing Reference Guide to help prioritize which mobile devices and OSs to test against, brought to you in partnership with Sauce Labs.

Android sqlite


  • 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

Analysts agree that a mix of emulators/simulators and real devices are necessary to optimize your mobile app testing - learn more in this white paper, brought to you in partnership with Sauce Labs.

Topics:

Published at DZone with permission of Avi Yehuda, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}