Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: prepare multiple value query #22817

Closed
Tommy-42 opened this issue Nov 20, 2017 · 2 comments
Closed

database/sql: prepare multiple value query #22817

Tommy-42 opened this issue Nov 20, 2017 · 2 comments

Comments

@Tommy-42
Copy link

@Tommy-42 Tommy-42 commented Nov 20, 2017

What version of Go are you using (go version)?

1.9

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

➜  go git:(dev) ✗ go env
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/tommypageard/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/6c/3td4zl350xjd08mht96p0tgm0000gq/T/go-build885240885=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

With the driver : database/sql

I want to prepare multiple value query, something like :

INSERT INTO my_table (column1, column2) VALUES
(?, ?),
(?, ?);

To do that I did :

theQuery := "INSERT INTO my_table (column1, column2) VALUES \n"

vals := []interface{}{}

for _, a := range array.Array {
    theQuery += "(?, ?),\n"
    vals = append(vals, a.value1, a.Value2)
}

//trim the last ,
theQuery = strings.TrimSuffix(theQuery, ",\n") + ";"

//prepare the statement
stmt, err := db.Prepare(theQuery) // error here
if err != nil {
    return err
}

defer stmt.Close()

//format all vals at once
res, err := stmt.Exec(vals...)
if err != nil {
   return err
}

What did you expect to see?

No error

What did you see instead?

error is return on stmt, err := db.Prepare(theQuery)
saying : syntax error at or near ","

Questions ?

I started to dev in go some month ago, I tried to search on google/stackoverflow for some example
without any luck, so I am here, trying to know if it is the database/sql driver that doesnt support this kind of prepare or it is me failing

@odeke-em
Copy link
Member

@odeke-em odeke-em commented Nov 20, 2017

@kardianos
Copy link
Contributor

@kardianos kardianos commented Nov 20, 2017

You're getting a SQL Syntax error. This is directly from your database engine.
Do the following:

  1. Print out fmt.Println(theQuery) once constructed.
  2. Try to run theQuery directly in the database engine.

But you may want to use bytes.Buffer AppendString, and then when ranging only add a comma when i != 0.

@kardianos kardianos closed this Nov 20, 2017
@golang golang locked and limited conversation to collaborators Nov 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.