For inserting into sqlite presently I have to follow these steps:
- Create contentValues i.e.
ContentValues contentValues = new ContentValues();
- Put column_name and Value
- lastly, call
sqLiteDatabase.insert(DATABASE_NAME,null,contentValues)
Problem is only in step 2,we have manually Columnname
and Columnvalue
for n number of times assuming I have n Columns to persist.
So, I wrote the following method thinking I can reuse it:
public void insert(Map tableMap){
ContentValues contentValues = new ContentValues();
Iterator tableMapIterator = tableMap.entrySet().iterator();
while(tableMapIterator.hasNext()){
Map.Entry mapEntry = (Map.Entry)tableMapIterator.next();
contentValues.put((String)mapEntry.getKey(), mapEntry.getValue());
}
sqLiteDatabase.insert(DATABASE_NAME,null,contentValues)
}
But the problem is that when I call mapEntry.getValue()
, the return type is Object for which contentValues.put
is not defined.
So, can anyone tell me any workaround so that I can use the above approach efficiently to do the data insertion.
NOTE : I want to write method so that I can use it for all data types in SQLITE.