有没有解决“未经授权”错误的方法?

huangapple go评论120阅读模式
英文:

Is there any solution to the not authorized error?

问题

sqlite3, err := sql.Open("sqlite3", "./map.gpkg")
if err != nil {
panic(err.Error())
}

_, err = sqlite3.Exec("select load_extension('mod_spatialite');")
if err != nil {
panic(err.Error())
}

当我尝试将spatialite扩展加载到sqlite3中时,它返回一个未授权的错误,我不知道如何修复它。

panic: not authorized

goroutine 1 [running]:
main.main()
/Users/u/project/project/golang_project/pack/sql/main.go:42 +0x145
exit status 2

英文:
sqlite3, err := sql.Open("sqlite3", "./map.gpkg")
if err != nil {
	panic(err.Error())
}

_, err = sqlite3.Exec("select load_extension('mod_spatialite');")
if err != nil {
	panic(err.Error())
}

When I try to load spatialite extension into sqlite3, it returns a not authorized error, I don't know how to fix it.

panic: not authorized

goroutine 1 [running]:
main.main()
        /Users/u/project/project/golang_project/pack/sql/main.go:42 +0x145
exit status 2

答案1

得分: 1

你需要使用SQLiteDriver的Extensions字段来注册扩展:

sql.Register("sqlite3_TestExtensionsError",
    &sqlite3.SQLiteDriver{
        Extensions: []string{
            "foobar",
        },
    },
)

可以参考这个示例:https://github.com/mattn/go-sqlite3/blob/1157a4212dcb650962563f67cd405794e9115b45/sqlite3_load_extension_test.go#L15

英文:

You need to register extension using Extensions field of SQLiteDriver:

sql.Register("sqlite3_TestExtensionsError",
	&sqlite3.SQLiteDriver{
		Extensions: []string{
			"foobar",
		},
	},
)

See this example: https://github.com/mattn/go-sqlite3/blob/1157a4212dcb650962563f67cd405794e9115b45/sqlite3_load_extension_test.go#L15

答案2

得分: 0

sql.Register("sqlite3_with_extensions",
&sqlite3.SQLiteDriver{
Extensions: []string{
"mod_spatialite",
},
})

db, err := sql.Open("sqlite3_with_extensions", "./map.gpkg")
if err != nil {
panic(err.Error())
}

我将代码更改如上所示,我的代码正常工作。

英文:
sql.Register("sqlite3_with_extensions",
	&sqlite3.SQLiteDriver{
		Extensions: []string{
			"mod_spatialite",
		},
	})

db, err := sql.Open("sqlite3_with_extensions", "./map.gpkg")
if err != nil {
	panic(err.Error())
}

i changed code as above and my code working fine.

huangapple
  • 本文由 发表于 2022年1月24日 12:19:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/70828966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定