Dileep Perla
Dileep Perla

Reputation: 1865

How to insert data into particular columns dynamically into a table in android

I have a table with columns : transaction_name | member1 | member2 | member3 | member4

In my activity, I will receive a String array of column names of this table, say columnarray[] = {"member1","member3","member4"}

and also I will receive a Integer array of column values to insert into those particular columns, say valuearray[] = {value1,value3,value4}

Now, in my activity, I have to write a query such that i have to insert these values(in integer array) into these columns(in string array) dynamically.

Simply, the following action should be performed :

"INSERT into tablename(member1,member3,member4) values(value1,value3,value4)"

As I will receive these column names and column values from arrays, how can i perform this insertion operation on my table dynamically?

Upvotes: 0

Views: 1982

Answers (1)

Boris Strandjev
Boris Strandjev

Reputation: 46953

Using an ordinary SQLiteDatabase:

ContentValues values = new ContentValues();
for (int i = 0; i < columnarray.length(); i++) {
    values.put(columnarray[i], valuearray[i]);
}
database.insert("tablename", null, values);

Upvotes: 1

Related Questions