1 回答
TA貢獻(xiàn)1827條經(jīng)驗 獲得超8個贊
您需要<FEATURE>將該指令中的擴展名替換為您希望從下表中啟用的擴展名(似乎 README 中有錯誤,并且sqlite_在示例中刪除了前綴;構(gòu)建標(biāo)簽確實是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 關(guān)注
- 737 瀏覽
添加回答
舉報
