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

How to load the json1 extension?

Open sanket28 opened this issue 5 years ago • 1 comments

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!

sanket28 avatar Jul 10 '20 19:07 sanket28

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)`

cyrixSpec avatar Jan 21 '21 12:01 cyrixSpec