Created
September 18, 2016 00:58
-
-
Save polefishu/62bffe0a6b54a1970e851b239c08d0e4 to your computer and use it in GitHub Desktop.
muilty db insert
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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