我正在使用Go,SQLX,postgres和go-swagger开发API。
在POST方法处理程序中,我获得了由swagger定义和验证的类型的请求正文。验证后,我想将其插入到postgres表中。
除了以下片段外,我没有找到太多有关该主题的文档:
sqlStatement := `
INSERT INTO users (age, email, first_name, last_name)
VALUES ($1, $2, $3, $4)
RETURNING id`
id := 0
err = db.QueryRow(sqlStatement, 30, "[email protected]", "Jonathan", "Calhoun").Scan(&id)
这意味着我需要描述要保留的结构的每个字段。
有没有一种方法仅将结构保存在表中?
db.save(struct)
这是在自述的例子:
// Named queries can use structs, so if you have an existing struct (i.e. person := &Person{}) that you have populated, you can pass it in as &person
tx.NamedExec("INSERT INTO person (first_name, last_name, email) VALUES (:first_name, :last_name, :email)", &Person{"Jane", "Citizen", "[email protected]"})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句