0

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.

Abhinav
  • 1,720
  • 4
  • 21
  • 33
  • 1
    `ContentValues` is already such a map. Adding a wrapper around it does not really make code simpler. – laalto Apr 23 '14 at 12:53
  • There is a funny trick using Parcelhttp://njzk2.wordpress.com/2013/05/31/map-to-contentvalues-abusing-parcelable/ – njzk2 Apr 23 '14 at 13:16
  • @laalto : Ya you are correct that `ContentValues` is already a map.But it is a pain to type all column names and its values every time.Rather I was thinking to pass a map with column_name as key and respective data as value on which we can iterate and persist.this will enable me to reuse the insert method throughout the application. – Abhinav Apr 24 '14 at 05:19
  • So, how do you plan to populate and maintain that map? Equal burden is just shifted to another place. – laalto Apr 24 '14 at 05:20

3 Answers3

0

I am not exactly sure if i get your question but i will try my best.

1) You can use some kind of already written ORM. - It can automatically detect field types.

2) You can write your own simple ORM to handle situations like this. When i want to automatically add object to DB, i inherit this table object from GenericTableObject, which has methods like getFieldsValues, getFieldsTypes etc... With help of these methods, it is fully automated.

You will probably spend a few hours by writing this generic table object but it is usefull. - It is all about java reflection.

kekesovo
  • 1
  • 1
0

try this one

public void addEntity(EntityClass e) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("Name",e.getmProductName());
        values.put("Price",e.getmProductPrice());
        values.put("Date",e.getmPurchaseDate());
        // Inserting Row
        db.insert(TABLE_Accounts, null, values);
        Log.d("insert", "success");
        Toast.makeText(mContext, "Info added Successfully", Toast.LENGTH_SHORT).show();

        db.close(); // Closing database connection




    }
0

The objects that will access your ContentMap will be verified by this method DatabaseUtils.getTypeOfObject()

Therefore, if you put anything in your ContentValue that is not one of the expected type, it will be assumed to be a String, and in bindArguments(), toString() will be called on it.

Now, assuming that all your object are either recognized valid types, or have sufficient String representation (for instance, a File object would give its path, which is sufficient to recreate it when you extract it from the database), there are ways to put an arbitrary Map in a ContentValue.

The trivial way is to use reflection to access the internal map, which is consistently named mValues across all versions of android.

Another, shorter (but slower) and clearer way, I find, is to use the Parcel mechanism. Indeed, ContentValue.writeToParcel only writes the internal map.

The entire code is here:

Parcel parcel = obtain();
parcel.writeMap(map);
parcel.setDataPosition(0);
ContentValues values = ContentValues.CREATOR.createFromParcel(parcel);

Detailed explanation on my blog : http://njzk2.wordpress.com/2013/05/31/map-to-contentvalues-abusing-parcelable/

njzk2
  • 38,969
  • 7
  • 69
  • 107