Question

I want to update my sqlite table and I use a request with conditionals my purpose is to set the name to the new value when the value is not null and keep the old value if the new value is null . I would like anybody check my request to see if there is something wrong cause I'm getting errors

The request below :

UPDATE user SET name = CASE WHEN (? = NULL) THEN user.name ELSE ? END  WHERE ID=? ;

The error I'm getting here

SQLITE_CONSTRAINT: NOT NULL constraint failed: user.name
Emitted 'error' event on Statement instance at:
] {
  errno: 19,
  code: 'SQLITE_CONSTRAINT'
}
Était-ce utile?

La solution

UPDATE user 
SET name = COALESCE(?, name) -- name to add if the value is not null
WHERE ID = ?                 -- id value of the record which must be updated
;
Licencié sous: CC-BY-SA avec attribution
Non affilié à dba.stackexchange
scroll top