Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
235 views
in Technique[技术] by (71.8m points)

android - update sql database with ContentValues and the update-method

I would like to update my SQL lite database with the native update-method of the SQLiteDatabase class of android.

ContentValues dataToInsert = new ContentValues();                          
dataToInsert.put("name", "flo");
dataToInsert.put("location", "flotown");
String where = "id" + "=" + id;
try{    
    db.update(DATABASE_TABLE, dataToInsert, where, null);
}
catch (Exception e){
    String error =  e.getMessage().toString();
}

but I get following error:

android.database.sqlite.SQLiteException: near "15": syntax error: , while compiling: UPDATE mytable SET location=?, name=? WHERE id=2010-09-21 15:05:36.995

I don′t know what should be the problem. Somehow the values do not arrive in the SQL statement. I did nearly the same with the insert method and that worked quite fine.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You're using the update function wrong. It should be like this:

String where = "id=?";
String[] whereArgs = new String[] {String.valueOf(id)};

db.update(DATABASE_TABLE, dataToInsert, where, whereArgs);

The Strings in the whereArgs array gets substituted in for each '?' in the where variable.

ie. if you had where = "name=? AND type=? then the first '?' would get replaced by whereArgs[0] and the second by whereArgs[1].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...