go-sqlite3
go-sqlite3 copied to clipboard
How to compile the go-sqlite3 for user authentication module
https://github.com/mattn/go-sqlite3/wiki/User-Authentication#features
Compile
To use the User authentication module the package has to be compiled with the tag sqlite_userauth. See Features.
I saw some information from this link, but it has no practical value.
package main
import (
"database/sql"
"fmt"
"log"
_ "github.com/mattn/go-sqlite3" // Import SQLite driver with userauth enabled
)
func main() {
// Create a SQLite database file
db, err := sql.Open("sqlite3", "./auth_example.db")
if err != nil {
log.Fatalf("Failed to open database: %v", err)
}
defer db.Close()
// Enable user authentication mode
_, err = db.Exec("PRAGMA user_version = 2;") // This enables the user authentication schema
if err != nil {
log.Fatalf("Failed to enable user authentication mode: %v", err)
}
// Create a user
createUserQuery := `PRAGMA user_add('admin', 'password123');`
_, err = db.Exec(createUserQuery)
if err != nil {
log.Fatalf("Failed to create user: %v", err)
}
fmt.Println("User 'admin' created successfully!")
// Authenticate the user
authQuery := `PRAGMA user_authenticate('admin', 'password123');`
row := db.QueryRow(authQuery)
var authResult int
err = row.Scan(&authResult)
if err != nil {
log.Fatalf("Failed to authenticate user: %v", err)
}
if authResult == 1 {
fmt.Println("User authenticated successfully!")
} else {
fmt.Println("Authentication failed!")
}
// Add another user
_, err = db.Exec("PRAGMA user_add('user1', 'mypassword');")
if err != nil {
log.Fatalf("Failed to create user1: %v", err)
}
fmt.Println("User 'user1' created successfully!")
// Authenticate the new user