我必須將我的資料庫密碼保護作為我學校的一項任務。例如,如果有人試圖訪問我的資料庫,它會詢問密碼。
我正在嘗試使用go-sqlite3包,并嘗試閱讀官方指南。
第一步是使用go build --tags <FEATURE>.
它給了我一個錯誤build .: cannot find module for path .
,我不知道為什么以及我們首先要構建什么。我也嘗試搜索實際示例,但沒有找到任何示例。
您能否向我解釋如何使用 golangs go-sqlite3 包為我的資料庫設定用戶身份驗證?
鏈接到包
uj5u.com熱心網友回復:
您需要<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
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/422598.html
標籤:
