Created
April 28, 2017 08:40
-
-
Save YanhaoYang/77302b28d01bb2f09379c51ecfc51dab to your computer and use it in GitHub Desktop.
SQL's IN query in Golang
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
func updateAll(ids []string) { | |
var params []string | |
var values []interface{} | |
for k, v := range ids { | |
params = append(params, fmt.Sprintf("$%d", k+1)) | |
values = append(values, v) | |
} | |
db := viper.Get("db").(*gorp.DbMap) | |
sql := "UPDATE stores SET active = false WHERE id IN (" + strings.Join(params, ",") + ")" | |
rs, err := db.Exec(sql, values...) | |
if err != nil { | |
panic(err) | |
} | |
rows, err := rs.RowsAffected() | |
if err != nil { | |
panic(err) | |
} | |
fmt.Printf("%d rows updated.\n", rows) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment