How to write common code for inserting data in android Sqlite

To enter in sqlite currently I have to follow these steps:

  • Create contentValues ​​i.e. ContentValues contentValues = new ContentValues();
  • Put the call_name and value
  • Finally call sqLiteDatabase.insert(DATABASE_NAME,null,contentValues)

The problem is only in step 2, we have manually Columnnameand Columnvaluefor n number of times, assuming that I have n columns that need to be saved.

So, I wrote the following method, thinking that 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.putit is not defined.

So, can anyone tell me some workaround so that I can effectively use the above approach for data entry.

. , SQLITE.

0
3

, ContentMap, DatabaseUtils.getTypeOfObject()

, - ContentValue, , , bindArguments(), toString() .

, , String (, File , , ) ContentValue.

- , mValues android.

, ( ) , , Parcel. , ContentValue.writeToParcel .

:

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

: http://njzk2.wordpress.com/2013/05/31/map-to-contentvalues-abusing-parcelable/

0

, , .

1) - ORM. - .

2) ORM , . , GenericTableObject, , getFieldsValues, getFieldsTypes .. .

, , , . - java.

0

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

All Articles