go-sqlite3
go-sqlite3 copied to clipboard
How to load the json1 extension?
Hello,
I'm trying to load the json1 extension. I know you can do it by specifying the --tags option in your go build command. But just wanted to know if its possible to load it during runtime. I tried doing it but no luck. Here's the code snippet:
import (
"database/sql"
"github.com/mattn/go-sqlite3"
)
func connect(dbPath string) error {
sql.Register("sqlite3_json1", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
if err := conn.LoadExtension("sqlite_json1", ""); err != nil {
return err
}
return nil
},
})
dbc, err := sql.Open("sqlite3", dbPath)
if err != nil {
return nil, err
}
return nil
}
Please advise. Thanks!
Hi,
first compile the extension gcc -g -fPIC -shared json1.c -o json1.so
now you can use the extension
` sql.Register("sqlite3_with_extensions", &sqlite3.SQLiteDriver{ Extensions: []string{ "json1", }, })
db, err := sql.Open("sqlite3_with_extensions", filename)`