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

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

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

英文:
  1. sqlite3, err := sql.Open("sqlite3", "./map.gpkg")
  2. if err != nil {
  3. panic(err.Error())
  4. }
  5. _, err = sqlite3.Exec("select load_extension('mod_spatialite');")
  6. if err != nil {
  7. panic(err.Error())
  8. }

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

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

答案1

得分: 1

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

  1. sql.Register("sqlite3_TestExtensionsError",
  2. &sqlite3.SQLiteDriver{
  3. Extensions: []string{
  4. "foobar",
  5. },
  6. },
  7. )

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

英文:

You need to register extension using Extensions field of SQLiteDriver:

  1. sql.Register("sqlite3_TestExtensionsError",
  2. &sqlite3.SQLiteDriver{
  3. Extensions: []string{
  4. "foobar",
  5. },
  6. },
  7. )

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())
}

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

英文:
  1. sql.Register("sqlite3_with_extensions",
  2. &sqlite3.SQLiteDriver{
  3. Extensions: []string{
  4. "mod_spatialite",
  5. },
  6. })
  7. db, err := sql.Open("sqlite3_with_extensions", "./map.gpkg")
  8. if err != nil {
  9. panic(err.Error())
  10. }

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:

确定