Skip to content

Instantly share code, notes, and snippets.

@polefishu
Created September 18, 2016 00:58
Show Gist options
  • Save polefishu/62bffe0a6b54a1970e851b239c08d0e4 to your computer and use it in GitHub Desktop.
Save polefishu/62bffe0a6b54a1970e851b239c08d0e4 to your computer and use it in GitHub Desktop.
muilty db insert
sqlStr := "REPLACE INTO users VALUES "
vals := []interface{}{}
for _, user := range users {
val := []interface{}{
user.ID,
user.FirstName,
user.LastName,
}
q := strings.Repeat("?,", len(val))
q = q[0:len(q) - 1]
sqlStr += fmt.Sprintf("(%s),", q)
vals = append(vals, val...)
}
sqlStr = sqlStr[0:len(sqlStr) - 1]
db := shared.Database.CommonDB()
//fmt.Println(sqlStr)
//fmt.Println(vals...)
stmt, err := db.Prepare(sqlStr)
if (err != nil) {
panic(err)
}
stmt.Exec(vals...)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment