How to prevent single quote on insert sql?

Viewed 33

I'm trying to insert as such:

query := `INSERT INTO logs (event_raw) FORMAT JSONEachRow {"event_raw": "?"}
_, err := s.db.ExecContext(ctx, query, event)

Where event is a string:

{\"id\":\"abcdefgh\"}

Unfortunately, I get an error because ? is replaced with '{\"id\":\"abcdefgh\"}' (wrapped with single quotes)

How can I prevent this behavior and just have: {"event_raw": "{\"id\":\"abcdefgh\"}"} ?

1 Answers

See my comment, but you might try:

query := "INSERT INTO logs (event_raw) FORMAT JSONEachRow ?"
arg := fmt.Sprintf(`{"event_raw": "%s"}`, event)
_, err := s.db.ExecContext(ctx, query, arg)
Related