6

I have a simple select statement:

Select * FROM X where X.name in ("bob", "joe") and X.phone='123'

That works fine in postgres,

In my Go code I have the following code:

var phone string = "123"
var names []string = []string{"bob", "joe"}
sqlStatement := `Select * FROM X where X.name in ($1) and X.phone=$2`
rows, sqlerr := db.Query(sqlStatement, names, phone)

but for some reason I error out from that sql.

unsupported Scan, storing driver.Value type into type *string

how can i use my names array in side the sqlstatement?

note: if i do a fmt.printf and paste the sql statement into postgres, i do get data back + if i take out the $1, and manually input the strings

2

2 Answers 2

6

Copying and pasting fragments from some working Go PostgreSQL code:

import (
    "database/sql"
    "github.com/lib/pq"
)

    query := `
            . . .
            WHERE code = ANY($1)
           . . .

        `

    codes := []string{"JFK", "LGA", "EWR"}
    rows, err := db.Query(query, pq.Array(codes))
Sign up to request clarification or add additional context in comments.

Comments

0

I solved this using http://jmoiron.github.io/sqlx/#inQueries

var phone string = "123"
var names []string = []string{"bob", "joe"}
sqlStatement := `Select * FROM X where X.name in (?) and X.phone=?`
sqlStatement, args, err := sqlx.In(sqlStatement, names, phone)
sqlStatement = db.Rebind(sqlStatement)
        rows, sqlerr := db.Queryx(sqlStatement, args...)

this now returns correctly.

another way to solve this was to use fmt.Sprintf() and converting the ? to %s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.