3

I have a following table:

sdb.execSQL("create table if not exists userprofile (id integer primary key, profilename text, user text);");

I want to update profilename field in Android SQLite.

So I used the following code:

ContentValues cv= new ContentValues();
cv.put("user",et4.getText().toString());
String admin="user1";
sdb.update("userprofile", cv, "profilename = " + admin, null);

The table have profilename value as user1. But when I execute this I am getting

03-14 09:12:55.851: E/SQLiteLog(26616): (1) no such column: user1

Please help me to resolve the problem.

Thanks in advance.

1
  • check values in content value... Commented Mar 14, 2013 at 9:24

2 Answers 2

1

Try to do it like this:

sdb.update("userprofile", cv, "profilename = ?", new String[] {"user1"});

I recommend this approach. Usage of placeholders solve problems like this. Your approach doesn't work because you are missing single quotes.

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

5 Comments

does it work like insert into or just update specific field?
@100kg this will update userprofile with profileName = 'user1'
can I somehow use ContentValues to insert into without writting insert into ?
@100kg for sure you can as couple of values cv.put("column_name", "new value"); insert into clause is db.insert() method.
@100kg of you want to write full query as "insert into Table values(?,?)" you need to use SQLiteStatement stmt = db.compileStatement(queryString); stmnt.bindString(1, "val1"); stmnt.bindString(2, "val2"); stmnt.executeInsert();
1

It should be -

ContentValues cv= new ContentValues();
cv.put("user",et4.getText().toString());
String admin="user1";
sdb.update("userprofile", cv, "profilename = '" + admin +"'", null);

Hope this help you.

1 Comment

your approach too is working fine.Thank you.So +1 to you also

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.