我是 Golang 的菜鳥,試圖創(chuàng)建 RestApi,它從郵遞員那里獲取請(qǐng)求并將其存儲(chǔ)在 MYSQL 中。在嘗試插入具有兩列(名稱和標(biāo)題)但在插入數(shù)據(jù)庫時(shí)一列始終為空的行時(shí),請(qǐng)告訴我如何添加多個(gè)字段。func createPost(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") stmt, err := db.Prepare("INSERT INTO posts(name,title) VALUES(?,?)") if err != nil { panic(err.Error()) } body, err := ioutil.ReadAll(r.Body) if err != nil { panic(err.Error()) } keyVal := make(map[string]string) json.Unmarshal(body, &keyVal) name := keyVal["name"] title := keyVal["title"] _, err = stmt.Exec(name, title) if err != nil { panic(err.Error()) }}JSON Request :{ "name":"John" "title":"Engineer"}
Golang RestApi Mysql 插入查詢
阿波羅的戰(zhàn)車
2022-06-13 15:40:12