0

I am having a problem using a string in a SQLite Update query.

This code is not working .

 database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_TASKNAME + " = " + strName, null);

here strName is a string.

But this code works fine

database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_ID + " = " +  id , null);

Here id is an integer.

What is the difference between them?

1 Answer 1

2

When you're doing it like that, you need to add quotation marks around your string:

database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_TASKNAME + " = \"" + strName + "\"", null);

But you should probably really look into parameterized queries (using the selectionArgs in update like this:

database.update(MySQLiteHelper.TABLE_NAME, values, MySQLiteHelper.COLUMN_TASKNAME + " = ?", new String[] { strName })

It will make your database safer against SQL injection attacks.

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

4 Comments

Can you please explain why i should add quotation marks around string?
+ always concate string and for integer it works fine
The quotes are how sql lite databases identify it's a string. Integers can be taken literally. Read up on sql and it should say something in a section on queries
Hello, i have been trying to make an update using parameters like that String whereClause = KEY_FIELD_CODE + “= ?” String whereArgs[] ={“123456″} But nothing works just update 0 rows, even if the rows who matches with the field exists. But the result change when i set the update statement like this: String whereClause = KEY_FIELD_CODE + “= 123456″ String whereArgs[] =null Does anyone knows why or in which cases we how. and the most important thing is how to fix that. Note: in this table in particular, im using a FTS3 para implementer full text search.. Thanks

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.