英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论