0

I need to save multiple tags in the single query:

valueStrings := make([]string, 0, len(tags))
valueArgs := make([]interface{}, 0, len(tags) * 3)
for _, tag := range tags {
    valueStrings = append(valueStrings, "(?, ?, ?)")
    valueArgs = append(valueArgs, tag)
    valueArgs = append(valueArgs, "NOW()")
    valueArgs = append(valueArgs, "NOW()")
}
stmt := fmt.Sprintf("INSERT INTO tags (name, created_at, updated_at) VALUES %s ON CONFLICT DO NOTHING;", strings.Join(valueStrings, ", "))
c.DB.Exec(stmt, valueArgs...)

Checking PostgreSQL logs:

ERROR:  syntax error at or near "," at character 58
STATEMENT:  INSERT INTO tags (name, created_at, updated_at) VALUES (?, ?, ?), (?, ?, ?) ON CONFLICT DO NOTHING;

I believe that Exec does not replace question marks with corresponding values.

Golang how do I batch sql statements with package database.sql

Community
  • 1
  • 1
nickbusted
  • 1,029
  • 4
  • 18
  • 30
  • 1
    Is this just [**“Operator does not exist: integer =?” when using Postgres with Golang**](http://stackoverflow.com/q/29887094/479863) in disguise? AFAIK the PostgreSQL interface for Go wants to use numbered placeholders (i.e. `$1`, `$2`, ...) instead of `?`. – mu is too short Feb 07 '16 at 22:34
  • @muistooshort Thank you:) – nickbusted Feb 07 '16 at 22:40

0 Answers0