У меня есть код, который будет обрабатывать вставку новых данных в базу данных mysql, я указал правильный контекст, оператор запроса и параметры значения. Но когда я пытаюсь выполнить запрос, я получаю такую ошибку
sql: expected 1 arguments, got 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}
}
Есть ли проблема в моем коде или проблема в моем драйвере? Спасибо
Решение проблемы
Попробуйте поставить вопросительные знаки в утверждении без апострофов:
statement:= "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES (?,?,?,?,?,?)"
Как видите, в вашем коде у вас есть только один аргумент ?без апострофов, поэтому драйвер ожидает один аргумент, а не шесть.
Комментариев нет:
Отправить комментарий