My God My God - 1 month ago 5
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 + "'");


OR

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


Thanks

Answer

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);
}