What does Go want for the second param in this SQL query.
I am trying to use the IN
lookup in postgres.
stmt, err := db.Prepare("SELECT * FROM awesome_table WHERE id= $1 AND other_field IN $2")
rows, err := stmt.Query(10, ???)
What I really want:
SELECT * FROM awesome_table WHERE id=10 AND other_field IN (this, that);
You can also use this direct conversion.
WARNING
This is method is vulnerable to SQL Injection. Use this method only if
awesome_id_list
is server generated.https://groups.google.com/d/msg/golang-nuts/vHbg09g7s2I/RKU7XsO25SIJ
It looks like you may be using the pq driver.
pq
recently added Postgres-specific Array support via pq.Array (see pull request 466). You can get what you want via:I think this generates the SQL:
Note this utilizes prepared statements, so the inputs should be sanitized.
Incase anyone like me was trying to use an array with a query, here is an easy solution.
get https://github.com/jmoiron/sqlx
Rather pedestrian and only to be used if server generated. Where UserIDs is a slice (list) of strings:
Query just takes varargs to replace the params in your sql so, in your example, you would just do
say, this and that of your second example were dynamic, then you'd do
If you have variable args for the "IN" part, you can do (play)