how to set an integer and a string value in android sqlite 'execSQL' statement

android, sqlite

Solution

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

Problem

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?

Original source

Related problems