I'm trying to write a query that would append my JSON variable("data") to a JSON field in my database. I have a table called cart with three fields inside: id type int
, status type varchar
and items type json
. So basically I'm trying to write a query where it would find my cart by the id and it would add an item to the end of my item field
so far what I have is this:
query, err := db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(@items, '$', 'data') where id = 1")
I know this is not enough. How can I add my variable instead of 'data' to this query? Could anyone help me?
Thank you.
My guess would be a parameterized update:
db.Exec("UPDATE cart SET items = JSON_ARRAY_APPEND(@items, '$', ?) where id = ?", data, 1)