我正在使用数据库/mysql和mysql驱动程序包来执行命令GRANT USAGE ON *.* TO 'test1'@'localhost' IDENTIFIED BY 'passoword
下面的代码抛出错误
错误1064(42000):SQL语法中有错误;检查与您的MariaDB服务器版本对应的手册,以获得正确的语法来使用near '?@?识别人?’在第1行
package main
import (
"database/sql"
"fmt"
"log"
"github.com/go-sql-driver/mysql"
)
var db *sql.DB
func main() {
// Capture connection properties.
cfg := mysql.Config{
User: "root",
Passwd: "example",
Net: "tcp",
Addr: "127.0.0.1:3306",
DBName: "test",
AllowNativePasswords: true,
}
// Get a database handle.
var err error
db, err = sql.Open("mysql", cfg.FormatDSN())
if err != nil {
log.Fatal(err)
}
pingErr := db.Ping()
if pingErr != nil {
log.Fatal(pingErr)
}
fmt.Println("Connected!")
_, err = db.Exec("GRANT USAGE ON *.* TO ?@? IDENTIFIED BY ?","test1", "%", "test1")
if err != nil {
fmt.Println(err)
}
}
我已经尝试db.Exec("GRANT USAGE ON *.* TO '?'@'?' IDENTIFIED BY '?'","test1", "%", "test1")
结果sql: expected 0 arguments, got 3
1条答案
按热度按时间vyu0f0g11#
mysql https://bugs.mysql.com/bug.php?id=28406中似乎有一个错误,它阻止在GRANT密码中使用变量。
只有使用fmt.Sprintf()替换变量包才能正常工作。