1 回答

TA贡献1827条经验 获得超8个赞
您需要<FEATURE>
将该指令中的扩展名替换为您希望从下表中启用的扩展名(似乎 README 中有错误,并且sqlite_
在示例中删除了前缀;构建标签确实是sqlite_userauth
)。
因此,启用用户身份验证将是go build -tags "sqlite_userauth"
.
在具有go-sqlite3
模块依赖项的项目中,只需确保使用-tags sqlite_userauth
.
这是一个最小的示例,展示了如何在项目中使用它:
mkdir sqlite3auth
cd sqlite3auth
go mod init sqlite3auth
touch main.go
main.go:
package main
import (
"database/sql"
"log"
"github.com/mattn/go-sqlite3"
)
func main() {
// This is not necessary; just to see if auth extension is enabled
sql.Register("sqlite3_log", &sqlite3.SQLiteDriver{
ConnectHook: func(conn *sqlite3.SQLiteConn) error {
log.Printf("Auth enabled: %v\n", conn.AuthEnabled())
return nil
},
})
// This is usual DB stuff (except with our sqlite3_log driver)
db, err := sql.Open("sqlite3_log", "file:test.db?_auth&_auth_user=admin&_auth_pass=admin")
if err != nil {
log.Fatal(err)
}
defer db.Close()
_, err = db.Exec(`select 1`)
if err != nil {
log.Fatal(err)
}
}
go mod tidy
go: finding module for package github.com/mattn/go-sqlite3
go: found github.com/mattn/go-sqlite3 in github.com/mattn/go-sqlite3 v1.14.10
# First build with auth extension (-o NAME is just to give binary a name)
go build -tags sqlite_userauth -o auth .
# then build without it
go build -o noauth .
./auth
2022/01/27 21:47:46 Auth enabled: true
./noauth
2022/01/27 21:47:46 Auth enabled: false
- 1 回答
- 0 关注
- 618 浏览
添加回答
举报