0

I have a table in SQLite where I want to update some of the columns. Here is my query:

String UPDATE_INFORMATION = "UPDATE " + TABLE_USER + " SET "
            + KEY_ADDRESS_OF_LIVING + " = " + addressOfLiving + ", "
            + KEY_PHONE + " = " + phoneNumber + ", "
            + KEY_IDNP + " = " + IDNP
            + " WHERE " + KEY_ID + " = 1";

The values in debug:

UPDATE user SET address = Efdsdfd, phone = 3797333, idnp = 2006002050245 WHERE id = 1

and the error message I get when running this query is:

android.database.sqlite.SQLiteException: no such column: Efdsdfd

Why it takes the "Efdsdfd" as column name?

3
  • try this UPDATE user SET 'address' = 'Efdsdfd', 'phone' = '3797333', 'idnp' = '2006002050245' WHERE 'id'=1 Commented May 8, 2017 at 13:04
  • 1
    @JineshFrancis ... and why are you quoting the coulmn names?????? Commented May 8, 2017 at 13:06
  • Anyway, you can use parametric SQL commands or queries and forget about quoting. Commented May 8, 2017 at 13:06

3 Answers 3

2

you are missing the simple quotes. Remember that when you are setting values for string columns, the values should be surrounded by simple quotes. For example :

UPDATE user SET address = Efdsdfd

should be

UPDATE user SET address = 'Efdsdfd'

Also my recommendation is that all constants that you use for the database query (simple quotes, commas, data type, etc) should be placed as Constants in a DbHelper or Constants file and not written.

For example:

String UPDATE_INFORMATION = "UPDATE " + TABLE_USER + " SET " + KEY_ADDRESS_OF_LIVING + " = " + addressOfLiving + ", "

Should be:

String UPDATE_INFORMATION = "UPDATE " + TABLE_USER + " SET " + KEY_ADDRESS_OF_LIVING + " = " + Helper.SIMPLE_QUOTES + addressOfLiving + Helper.SIMPLE_QUOTES + Helper.COMMA

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

4 Comments

Thank you for your response. It would be much easier to write a simple quote that to call the value from a helper class :) The best solution would be to call the SQLite update function rather than running the manual written query, but I do this for testing purposes
@AndrewT "The best solution would be to call the SQLite update function" i completely agree with you. But who am i to judge) =) . Have a good testing
Or you can use parametric SQL commands or queries and forget about quoting.
@Rotwang also true
2

You have to put TEXT values inside single quotes.

2 Comments

Or you can use parametric SQL commands or queries and forget about quoting.
I just wanted to see how to run queries as I will have to perform multiple operations that I will be unable to achieve using queries. But for this query you are completely right!
1
UPDATE user SET address = Efdsdfd, phone = 3797333, idnp = 2006002050245 WHERE id = 1

The above query is not valid as address is type of VARCHAR or TEXT. You have to put TEXT values inside single quotes.

Updated query string:

String UPDATE_INFORMATION = "UPDATE " + TABLE_USER + " SET "
        + KEY_ADDRESS_OF_LIVING + " = '" + addressOfLiving + "', "
        + KEY_PHONE + " = " + phoneNumber + ", "
        + KEY_IDNP + " = " + IDNP
        + " WHERE " + KEY_ID + " = 1";

Result will be as

UPDATE user SET address = 'Efdsdfd', phone = 3797333, idnp = 2006002050245 WHERE id = 1

3 Comments

Thank you! This seems to have solved my problem. I will reinstall the app and as one of the columns in my sqlite was not created but most sure it was the problem
yes! I had to wait 10 minutes until I can accept the answer. Thanks!
Or you can use parametric SQL commands or queries and forget about quoting.

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.