ActiveRecord

public class ActiveRecord

A simple set of helper methods to query for data on android sqlite database.

To get the instance of theis Active record use the getInstance method, this method takes just one parameter. an instance of SQLiteDatabase.

this class is implemented as a singleton meaning only one instance of ActiveRecord ever exists in your application life time.

NB:: the instance of SQLiteDatabase passed in getInstance method is destroyed once the garbage collector destroys the instance of the ActiveRecord.

Methods

all

public List<Map> all(String tableName, String[] queryCols)

Returns an list of maps, where the map represents each record in the database.

with keys of the map as the column name and values of the map as the values of the respective columns.

something like this:

[{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]

Parameters:
  • tableName
  • queryCols

all

public List<Map> all(String tableName)

Returns an list of maps, where the map represents each record in the database.

with keys of the map as the column name and values of the map as the values of the respective columns.

something like this:

[{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]

Parameters:
  • tableName

exists

public boolean exists(String tableName, String field, String value)

Returns value of the single column requested.

exists

public boolean exists(String sql, String[] params)

Returns value of the single column requested.

finalize

protected void finalize()

find

public List<Map> find(String sql, String[] args)

Returns an list of maps, where the map represents each record in the database.

with keys of the map as the column name and values of the map as the values of the respective columns.

something like this:

[{id:1, username:ken, age:50}, {id:2, username:matt, age:70}]

Parameters:
  • sql
  • args

get

public HashMap get(String sql, String[] params)

Returns a Map representing a single record based on the query.

Parameters:
  • sql
  • params – parameters to bind to the query

getDb

public SQLiteDatabase getDb()

REturn instance of SQLiteDatabase that the activerecord instance is using.

getInstance

public static ActiveRecord getInstance(SQLiteDatabase database)

Returns an instance of the the activerecord class

Parameters:
  • database

getScalarInt

public int getScalarInt(String sql, String[] params)