go-sqlite3 icon indicating copy to clipboard operation
go-sqlite3 copied to clipboard

Fix json example

Open Jaculabilis opened this issue 11 months ago • 0 comments

Because of sqlite's flexible typing, even though the column was declared as jsonb, the values are stored as the TEXT-typed json because they're converted to strings by Value/Scan. If the table is strict with a BLOB column, the example fails because of the type mismatch. This can be fixed by using the jsonb() function to convert incoming string-typed json and the json() function to convert outgoing binary-typed jsonb. The example is expanded to show both of these approaches.

Note that both approaches use the same string-typed marshalling functions because the conversion to jsonb occurs within sqlite3, not within the Go code. SQLite docs state that the binary format is internal and applications shouldn't try to generate it: https://sqlite.org/json1.html#jsonb

Fixes #1312.

Jaculabilis avatar Jan 21 '25 14:01 Jaculabilis