0

It would be helpful if someone could tell me correct syntax for updating tables in sqlite. I want to update table using sqlite but I am getting the following error:

android.database.sqlite.SQLiteException: near "ON": syntax error (code 1): , while compiling: INSERT INTO cost (service_type,cost) VALUES('1000','Oil and Filter Change') ON DUPLICATE KEY UPDATE service_type='Oil and Filter Change',cost='1000';

Code:

public void UpdatePrice(String ser_type, String cost)
{
    String query = "INSERT INTO " + TABLE_COST +
            " (" + COLUMN_SERVICE_TYPE + "," + COLUMN_COST +") VALUES('"+
            cost + "','" +ser_type +"') ON DUPLICATE KEY UPDATE "+
            COLUMN_SERVICE_TYPE + "='" + ser_type + "',"+
            COLUMN_COST + "='" + cost + "';";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    // Move to first row
    cursor.moveToFirst();

    cursor.close();
    db.close();
}
1
  • SQLite doesn't support that syntax (see here). INSERT OR REPLACE may do what you want, but be aware that replace means replace, and the rowid may change. You also probably ought to be using parameterised queries, but that's another matter. Commented Mar 21, 2017 at 14:18

1 Answer 1

0

ON DUPLICATE KEY UPDATE isn't a valid syntax of SQLite as of now. But since you're interested, you can perhaps achieve the same effect with different sytax described in here.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.