0

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!

3 Answers 3

4
    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 
Sign up to request clarification or add additional context in comments.

3 Comments

That's ok,but If I want to use this method to insert data in other tables too,which don't have this rows?Is there a way to put key-value,without writing all of them as params in method head section?
ya use can create a seperate string array for different different tables like String[] fieldtable={"seed","id","name","lang"}; private static boolean execQuery(String tableName,String[] field, String seed,String id,String name, String lang
just pass the contentvalues as parameter in your method
2

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);

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.