Reputation: 14585
I have a little problem with inserting data in sqlite in Android. I wrote a method which do that with ContentValues
,but it's not working properly. Here is the method :
DatabaseHelper.class
public boolean executeQuery(String tableName,String keys,Object value){
return execQuery(tableName,keys,value);
}
private static boolean execQuery(String tableName,String key,Object value){
sqliteDb = instance.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(key, value.toString());
sqliteDb.insert(tableName, null, values);
return true;
}
And I'm using this like that :
dbHelper.executeQuery("users", "seed", seed); // string
dbHelper.executeQuery("users", "id", id); // int
dbHelper.executeQuery("users", "name", name); // string
dbHelper.executeQuery("users", "lang", lang); // string
And the problem is that I this method insert all values as single row in database,but I want all data to be a part of one row.
What I have to do to get the things to work correctly? I'm not really good with sqlite,so please excuse me if my question is a little silly...
Thanks in advance!
Upvotes: 0
Views: 2166
Reputation: 42026
private static boolean execQuery(String tableName,String seed,String id,String name, String lang){
sqliteDb = instance.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("seed", seed);
values.put("id", id);
values.put("name", name);
values.put("lang", lang);
sqliteDb.insert(tableName, null, values);
return true;
}
dbHelper.executeQuery("users",seed,id, name,lang); // string
EDITED
private void execQuery(String tableName,ContentValues val)
{
sqliteDb = instance.getWritableDatabase();
sqliteDb.insert(tableName, null, val);
return true;
}
call
ContentValues values = new ContentValues();
values.put("seed", seed);
values.put("id", id);
values.put("name", name);
values.put("lang", lang);
dbHelper.executeQuery("users",values); // string
Upvotes: 4
Reputation: 1063
you have to pass string key array and Object array
dbHelper.executeQuery("users", /*Array of seed,id,name,lang*/, /*Object Array of their value*/);
and use
values.put(key[i], value[i].toString()); for all i=0 to n
Upvotes: 1
Reputation: 72341
You should use the same ContentValues
object to add all your desired columns. Like this:
Content values= new ContentValues();
values.add("seed",seed);
values.add("id",id);
values.add("name",name);
values.add("lang",lang);
sqliteDb.insert(tableName,null,values);
Upvotes: 2