My God My God - 1 year ago 55
SQL Question

Update table field value to null programmatically

It is well known that how to set the field value to null by a simple query like -

UPDATE your_table
SET your_column = NULL
WHERE id = 1;

But pro-grammatically, which one is correct way to update the field value to null -

db.execSQL("UPDATE your_table SET your_column='" + null + "WHERE id='" + myid + "'");


db.execSQL("UPDATE your_table SET your_column= NULL WHERE id='" + myid + "'");


Answer Source

I would go with PreparedStatement.

String query="UPDATE  your_table SET your_column= ? WHERE id=?");
PreparedStatement stmnt = conn.prepareStatement(query);
if(colyouAretryingtopass == null){]
  stmnt.setNull(1, Types.VARCHAR);