我有處理將新數(shù)據(jù)插入數(shù)據(jù)庫 mysql 的代碼,我已經(jīng)放置了正確的上下文、查詢語句和值參數(shù)。但是當(dāng)我嘗試執(zhí)行查詢時(shí),出現(xiàn)了這樣的錯(cuò)誤sql: expected 1 arguments, got 6這是我的司機(jī)"github.com/go-sql-driver/mysql"這是我的數(shù)據(jù)庫連接語句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}}我的代碼有問題還是我的驅(qū)動(dòng)程序有問題?謝謝
1 回答

繁華開滿天機(jī)
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試將問號(hào)放在沒有撇號(hào)的語句中:
statement := "INSERT INTO product (id_product, user, field, judul, negosiasi, createdAt) VALUES (?, ?, ?, ?, ?, ?)"
如您所見,在您的代碼中只有一個(gè)?
沒有撇號(hào),因此驅(qū)動(dòng)程序需要一個(gè) arg 而不是六個(gè)
- 1 回答
- 0 關(guān)注
- 177 瀏覽
添加回答
舉報(bào)
0/150
提交
取消