golang mysql不替换占位符

时间:2017-10-05 05:30:27

标签: mysql go

我正在使用sqlx和mysql驱动程序,但是当我执行一个insert语句时,我得到了“错误1064:你的SQL语法中有错误;”

import (
    _ "github.com/go-sql-driver/mysql"
    "github.com/jmoiron/sqlx"
)
func main(){
db, _ := sqlx.Connect("mysql", "connetionStr")
query := fmt.Sprintf("insert into bot(poster_id,name,welcome_msg,exit_msg) values(?,?,\"%s\",\"%s\")", "hello", "bye")
result := db.MustExec(query, 1, "bot_name")

}

语法当然没错。问题在于db.MustExec函数。为什么不替换?占位符

1 个答案:

答案 0 :(得分:0)

试试这个:

query := fmt.Sprintf("insert into bot(poster_id,name,welcome_msg,exit_msg) values(?,?,'%s','%s')", "hello", "bye")
result := db.MustExec(query, 1, "bot_name")