0

I was trying to duplicate this SQLite statement from the line of code below:

Cursor cursor = db.rawQuery("update tbl_details SET ticket = replace(ticket, " + tempID + ", " + ticket + ")", null);

to this one:

SQLiteDatabase db = this.getWritableDatabase();
        ContentValues cv = new ContentValues();
        cv.put("ticket", "replace(ticket, " + tempID + ", " + ticket + ")");
        db.update("tbl_details", cv, null, null);
        return true;

What I am trying to do is to get a New ID and replace all instances of the old temporary ID in the database. But the code above is changing all the records in ticket column.

Please help. Thank you!

2
  • Well: update(...): whereClause the optional WHERE clause to apply when updating. Passing null will update all rows. You pass null, hence all rows are affected. Commented Jan 22, 2016 at 14:40
  • What should I put in the ContentValues cv? Commented Jan 22, 2016 at 14:42

1 Answer 1

2

You can use ContentValues to bind literal values only, not expressions like replace(...).

To run the raw UPDATE SQL, just use execSQL() instead of rawQuery(). rawQuery() alone won't actually run the code until the returned Cursor is moved.

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

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.