2

I am trying to set an integer and a string value in execSQL statement :

mydb.execSQL("UPDATE emptable set age=? where name=?",new int[] {emp_age},new String[] {emp_name});

where emp_age is an integer and emp_name is a String. But this is not working.How can i achieve this?

3 Answers 3

11

Sorry, but I'm not happy with the given answers, and I'll explain why.

@hotveryspicy answer is not only ugly but also dangerous. I'll remind you of the possibility of an SQL-injection, which can cost you your stored data. The same is true for @john smith answer.

@user1318091 answer is incorrect for the same reason that makes all of these answers (including your given example-code) wrong. If you read the documentation, you'll notice that it says:

Execute a single SQL statement that is NOT a SELECT/INSERT/UPDATE/DELETE.

on the first line. It also lists alternatives for the above mentioned operations:

For UPDATE statements, use any of the following instead.

  • update(String, ContentValues, String, String[])
  • updateWithOnConflict(String, ContentValues, String, String[], int)

If you choose to use the simple update()-method, your query will look like this (untested!):

ContentValues values = new ContentValues();
values.put("age", age); // your integer...
mydb.update("emptable", values, "name=?", new String[]{emp_name});

Also check this question for the correct "where"-syntax: update sql database with ContentValues and the update-method

Sign up to request clarification or add additional context in comments.

2 Comments

i appreciate, +1 goes to you. Thanks for giving such useful information.
i appreciate your answer. i had changed my code. And thanks for such useful info.
1

the sdk says

bindArgs: only byte[], String, Long and Double are supported in bindArgs

But if you want to use bindArgs, then you can try this

mydb.execSQL("UPDATE emptable set age=? where name=?",new String[] {String.valueOf(emp_age)},new String[] {emp_name});

1 Comment

So, the value of "String.valueOf(emp_age)" will be a perfect Integer or Integer stored as string ??
-1
 String sql = "UPDATE emptable  SET age = "+age + " WHERE name = '" + emp_name + "';";
 db.execSQL(sql);

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.