Reusable generic function to save any data in SQLLiteDatabase in Android using ReflectionAPI

Post date: Jun 28, 2013 11:07:22 AM

You know I'm working very hard to find the laziest way to do the jobs:

Recently I was involved an Android project where it includes SQLLiteDatabases and more tables. 

If you are Android developer you might know that each table we suppose to write individual function to insert the data in to it. Since each table has different rows in nature.

Like for ex: If I've a table for login:

 username    

 password

 Role

Then Android insert function will be like this:

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("username","Boobalan");

values.put("password", "great@123");

values.put("role", "Admin");

database.insert("login", null, values);

database.close();

The same way I had one more table called Customer:

Insert function for the able table is like this

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

values.put("name","Boobalan");

values.put("address", "Chennai");

values.put("dob", "26/feb/2014");

values.put("mobieNo", "29234902348");

database.insert("Customer", null, values);

database.close();

So for each table i have to write a separate function to do insert operation. It makes me frustrated and looks for alternative. So I end up with "ReflectionAPI"

Just I have created POJO class for each table, column name and type should match with class member variable name and type

Generic ReflectionAPI method to insert data into the table:

public void insertCustomData(Object clazz,String tableName) {

try {

SQLiteDatabase database = this.getWritableDatabase();

ContentValues values = new ContentValues();

@SuppressWarnings("rawtypes")

//Get given class 

Class clazzClass=clazz.getClass();

//Get declared fields 

Field[] arrFields= clazzClass.getDeclaredFields();

for (Field field : arrFields) {

//Make it accessible 

field.setAccessible(true);

Log.d("Custom Class","Field Name: "+field.getName() +" Value :"+field.get(clazz));

//Get class type and according put the values in content  

Class<?> typeClass = field.getType();

if (typeClass==String.class) {

values.put(field.getName(), (String) field.get(clazz));

}else if(typeClass==Boolean.class){

values.put(field.getName(), (Boolean) field.get(clazz));

}else if(typeClass==Integer.class){

values.put(field.getName(), (Integer) field.get(clazz));

}else if(typeClass == Double.class){

values.put(field.getName(), (Double) field.get(clazz));

}

}

//Insert values into the table

database.insert(tableName, null, values);

//Close the database

database.close();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

My New Login POJO Class:

public class CUser {

String username;

String password;

String role;

public String getUsername() {

return username;

}

public void setUsername(String username) {

this.username = username;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getRole() {

return role;

}

public void setRole(String role) {

this.role = role;

}

}

The way to call generic function is mentioned below:

CUser couCUser=new CUser();

couCUser.setUsername("Muruga");

couCUser.setPassword("great@123");

couCUser.setRole("Admin");

 //Call our generic function to insert the values into table

insertCustomData(couCUser, "login");

By this way you can avoid redundant codes in your application. Hope you enjoy this article. 

Please let me know if you need any help