21

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.

1
  • 6
    I know this is old post but you should never user "id" use "_id" instead. "_id" is a common id for databases in Android and recomended. Commented Jan 19, 2012 at 20:59

4 Answers 4

49

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].

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

2 Comments

"Cannot invoke toString() on the primitive type long"
@Mirko: Use Long.toString(id) instead!
4

Actually, you just need to add apostrophes to your where clause. So it ought to be:

String where = "id='" + id + "'"

(note: however, this is not best practice, as it theoretically leaves open to injection attacks)

Comments

1

I have an other approach

    public boolean updateEmployee(TalebeDataUser fav) {

    SQLiteDatabase database = dbHelper.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(DBHelper.COLUMN_ID, fav.getId());
    contentValues.put(DBHelper.COLUM_AD, fav.getAd());
    contentValues.put(DBHelper.COLUMN_NUMARA, fav.getNumara());
    contentValues.put(DBHelper.COLUMN_YURD_ID, fav.getYurtID());
    contentValues.put(DBHelper.COLUMN_EGITIM_ID, fav.getEgitimTur());
    contentValues.put(DBHelper.COLUMN_TEL, fav.getTel());
    contentValues.put(DBHelper.COLUMN_EMAIL, fav.getEmail());
    contentValues.put(DBHelper.COLUMN_ADDRESS, fav.getAdres());

    String whereClause = DBHelper.COLUM_AD + " = ? AND " + DBHelper.COLUMN_NUMARA + " = ? ";
    final String whereArgs[] = {fav.getAd(), String.valueOf(fav.getNumara())};// old nameler taranıyor
    int affectedRows = database.update(DBHelper.TABLE_NAME_OGR, contentValues, whereClause, whereArgs);
    return affectedRows > 0;
}

Comments

0

Actually what exactly you written is correct. The syntax is correct. But you have to check these. String where = "id" + "=" + id; In the above declaration "id" should be type number and id should be int. And if id is a type of TEXT then follow @Adam javin answer.

Comments

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.