在下面的示例中,我使用Golang中的json_extract(...)
驱动程序使用go-sqlite3
。
package main
import (
_ "github.com/mattn/go-sqlite3"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"fmt"
"encoding/json"
"net/http"
)
func main() {
var db *gorm.DB
type Session struct {
gorm.Model
SessionID string `json:"session_id"`
Options string `json:"options"`
}
db, err := gorm.Open("sqlite3", "./app.db")
if err != nil {
fmt.Println(err)
}
defer db.Close()
db.AutoMigrate(&Session{})
var m map[string]int
m = make(map[string]int)
m["a"] = 1
m["b"] = 2
m["c"] = 3
js, err := json.Marshal(m)
sess := Session{
SessionID: "test",
Options: string(js),
}
db.Create(&sess)
db.Save(&sess)
var b = &Session{}
type Result struct {
Options string
}
// JSON test
var res Result
db.Raw("SELECT json_extract(options, '$.a') as Options from sessions").Scan(&res)
fmt.Println(sess.ID)
fmt.Println(res)
}
问题是,在重新构建JSON1驱动程序时,我无法激活go-sqlite3
模块。它将在undefined function: json_extract
行上出错。
在任何情况下,我都知道为了支持JSON,github.com/mattn/go-sqlite3
必须用-flags "sqlite_json1"
编译。我试过几种变体:
go build -a -flags "sqlite_json1" github.com/mattn/go-sqlite3
go install -a -flags "sqlite_json1" github.com/mattn/go-sqlite3
go build -a --flags "sqlite_json1" github.com/mattn/go-sqlite3
go install -a --flags "sqlite_json1" github.com/mattn/go-sqlite3
flags
中有更多的变体,如sqlite_json
、json
、json1
等。没有什么能消除未定义的函数错误。如何正确地重建go-sqlite3
显然,在此之后,我也重新构建了自己的代码。
一些信息:
go版本:go version go1.13.1 linux/amd64
(平台:Kubuntu18.04)
go-sqlite3 3版本: github.com/mattn/go-sqlite3 3当前母版
gorm版: github.com/jinzhu/gorm当前主版
发布于 2019-10-10 06:46:50
根据go-sqlite3
的文档,下列任何一个标志都可以工作:
sqlite_json
sqlite_json1
json1
下面是sqlite3_opt.json1.go
的内容,它定义了包含该文件的标记。
// Copyright (C) 2014 Yasuhiro Matsumoto <mattn.jp@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// +build sqlite_json sqlite_json1 json1
package sqlite3
/*
#cgo CFLAGS: -DSQLITE_ENABLE_JSON1
*/
import "C"
参考资料:https://github.com/mattn/go-sqlite3/blob/master/sqlite3_opt_json1.go
另外要指出的是,您在--flags
命令中使用了go build
,但是尝试一下--tags
。
https://stackoverflow.com/questions/58303609
复制相似问题