GoLang和MySQL DB

时间:2019-05-15 08:25:13

标签: go mysql-connector

使用github.com/go-sql-driver/mysql包在Go中执行MySQL命令是否有更简单的方法?

本质上,这是我正在使用的当前命令:

db.Exec("INSERT INTO table1 (id, title, name, dob, address, email, notes) VALUES (?, ?, ?, ?, ?, ?, ?)", id, title, name, dob, address, email, notes)

我将如何使用此命令:

var people []people

for _, person := range people {
     db.Exec("INSERT INTO table1 (id, title, name, dob, address, email, notes) VALUES (?, ?, ?, ?, ?, ?, ?)", person.id, person.title, person.name, person.dob, person.address, person.email, person.notes)
}

1 个答案:

答案 0 :(得分:0)

https://godoc.org/github.com/jmoiron/sqlx#NamedExec使它稍微好一些。可能是这样的:

result, err := db.NamedExec(`INSERT INTO table1 (id, title, name, dob, address, email, notes) VALUES (:id, :title, :name, :dob, :address, :email, :notes)`,
person)

请查看我的model.go example,以获取更多详细信息。