Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how to build go-sqlite3 to include the json feature ? #710

Closed
jiehuaou opened this issue Apr 19, 2019 · 8 comments
Closed

how to build go-sqlite3 to include the json feature ? #710

jiehuaou opened this issue Apr 19, 2019 · 8 comments

Comments

@jiehuaou
Copy link

jiehuaou commented Apr 19, 2019

Here is my install procedure:

  1. go get github.com/mattn/go-sqlite3
  2. go build -tags json1 github.com/mattn/go-sqlite3
  3. go install github.com/mattn/go-sqlite3

after build and install , I run my code like this:

// open database
database, _ := sql.Open("sqlite3", "file:test.db?cache=shared&mode=memory")
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS people (id INTEGER PRIMARY KEY, key TEXT, value TEXT, valueJson)")
statement.Exec()

``
// INSERT data
statement, _ = database.Prepare("INSERT INTO people (key, valueJson) VALUES (?, ?)")
statement.Exec("Nic", {"a":1, "b": "Raboy"})
statement.Exec("Hello", `{"a":2, "b": "World"}`)

sqlStr := `SELECT id, key, valueJson FROM people where json_extract(valueJson, "$.a")=2`

rows, err := database.Query(sqlStr)
if err != nil {
	fmt.Println(err)
}

`
then I got this error
no such function: json_extract
panic: runtime error: invalid memory address or nil pointer dereference

what did I miss ?

@rittneje
Copy link
Collaborator

rittneje commented Apr 19, 2019

after build and install , I run my code like this

That's not how Go works. There is no need to install dependencies in advance, as Go will always compile your application in full. I'm assuming that when you compiled your actual application (via go run, go build, or go install), you did not specify the json1 build tag. Consequently, your application does not include the feature in question, because the version of this library that was actually compiled in was not told to include it.

@jiehuaou
Copy link
Author

jiehuaou commented Apr 30, 2019

I have fixed it by adding this line in sqlite3.go

#cgo CFLAGS: -DSQLITE_ENABLE_JSON1

then rebuild and install,

@glebarez
Copy link

glebarez commented Sep 4, 2021

same issue
your solution does seem like a hammer-hit.
why isn't normal way working ?

@mattn
Copy link
Owner

mattn commented Sep 4, 2021

Some user want json1 but someone not.

@mattn
Copy link
Owner

mattn commented Sep 4, 2021

If many users want json1 to be the default, then I will make it the default.

@otoolep
Copy link
Contributor

otoolep commented Sep 4, 2021

Rebuilding the source code with the functionality you want, if it's not enabled by default, seems perfectly fine to me. I don't know why it's considered a big deal. It's the same with the SQLite source itself -- not everything is on by default. Thank you, @mattn and @rittneje for providing mattn/go-sqlite3 in the first place!

I recently did enable JSON support in rqlite, but only because the SQLite library that comes with Ubuntu has it enabled, so it seems mainstream enough. But still, building your own version is not a big deal IMHO.

@glebarez
Copy link

glebarez commented Sep 10, 2021

Rebuilding the source code with the functionality you want, if it's not enabled by default, seems perfectly fine to me.

I agree with you here.
I think this whole thread has been drenched with misunderstanding 😄

The behaviour I noticed on my side, that made me write a post here, is as follows.
I had to run every go command (go run / go test) with explicit "json1" build flag,
otherwise go would just rebuild this library with default settings, before executing, thus resulting in errors due to missing json functionality.
This behaviour can be clearly confirmed using -v flag in "go run" or "go test"

I suspect it's rather go's behavioural issue.

@kerneltravel
Copy link

kerneltravel commented Apr 8, 2024

Notes on how to specify the json1 build tag for building to enable the json support .
my go project import github.com/mattn/go-sqlite3 ( version @v2.0.3 ). so build my project with go build -tags json1 , after then ,run executable and the no such function: json_extract error gone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants