在使用`execContext`执行插入操作时出现错误(Golang)。

huangapple go评论79阅读模式
英文:

Error when insert using execContext golang

问题

我有一段代码,用于将新数据插入到MySQL数据库中,我已经提供了正确的上下文、查询语句和参数值。但是当我尝试执行查询时,我得到了以下错误:

sql: 预期 1 个参数,但提供了 6 个

这是我的驱动程序:

"github.com/go-sql-driver/mysql"

这是我的数据库连接语句:

connResult, err := sql.Open("mysql", os.Getenv("MY_SQL_URL"))

这是我的代码:

func (pr productRepo) AddProduct(c *gin.Context, req product.AddProduct) *model.RepoResponse {
	var negotiate int8
	ctx, cancel := context.WithTimeout(c, 10*time.Second)
	identity := library.Identity(c)
	currentTime := library.Time().CurrentDateTimeDbFormat()

	defer cancel()

	id := uuid.New()
	if req.Negotiate {
		negotiate = 1
	}
	statement := "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES (?, ?, ?, ?, ?, ?)"

	result, errInsert := pr.conn.ExecContext(ctx, statement, id, identity.GetUserID(), req.Field, req.Title, negotiate, currentTime)
	if errInsert != nil {
		fmt.Println(errInsert)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	}

	inserted, errRows := result.RowsAffected()
	if errRows != nil {
		fmt.Println(errRows)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	} else if inserted == 0 {
		fmt.Println("Inserted value ", inserted)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	}
	return &model.RepoResponse{Success: true, Data: id}
}

我的代码有问题吗?还是驱动程序有问题?谢谢。

英文:

I have code which will handle inserting new data into database mysql, I have put right context, query Statement, and parameters of value. But when I try to execute the query, I got error like this

sql: expected 1 arguments, got 6

this is my driver

"github.com/go-sql-driver/mysql"

this is my db connection statement

connResult, err := sql.Open("mysql", os.Getenv("MY_SQL_URL"))

and this is my code

func (pr productRepo) AddProduct(c *gin.Context, req product.AddProduct) *model.RepoResponse {
	var negotiate int8
	ctx, cancel := context.WithTimeout(c, 10*time.Second)
	identity := library.Identity(c)
	currentTime := library.Time().CurrentDateTimeDbFormat()

	defer cancel()

	id := uuid.New()
	if req.Negotiate {
		negotiate = 1
	}
	statement := "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES ('?', '?', '?', '?', ?, '?')"

	result, errInsert := pr.conn.ExecContext(ctx, statement, id, identity.GetUserID(), req.Field, req.Title, negotiate, currentTime)
	if errInsert != nil {
		fmt.Println(errInsert)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	}

	inserted, errRows := result.RowsAffected()
	if errRows != nil {
		fmt.Println(errRows)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	} else if inserted == 0 {
		fmt.Println("Inserted value ", inserted)
		return &model.RepoResponse{Success: false, Msg: "Failed to add product"}
	}
	return &model.RepoResponse{Success: true, Data: id}
}

Is there any problem in my code or it is from my driver? Thank you

答案1

得分: 0

尝试将语句中的问号放在没有撇号的位置:

statement := "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES (?, ?, ?, ?, ?, ?)"

正如你所看到的,在你的代码中,只有一个没有撇号?,所以驱动程序期望有一个参数而不是六个。

英文:

Try to put the question marks in the statement without the apostrophes:

statement := "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES (?, ?, ?, ?, ?, ?)"

As you can see, in your code you have only one ? without the apostrophes, so the driver expects one arg and not six

huangapple
  • 本文由 发表于 2022年4月13日 01:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/71846680.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定