4

I am trying to perform the following type of query in golang using the sql library:

    rows, err := db.Query("select * from someTable where age = ? and hairColor = ?", age,haircolor)

But get the following sort of error:

    Error occured: sql: expected 1 arguments, got 2

How do I perform an SQL select statement in golang where there are multiple values in the WHERE clause?

2
  • which db are you using? Commented Nov 23, 2019 at 8:25
  • database/sql and mysql. Commented Dec 17, 2019 at 5:09

1 Answer 1

3

The answer is to use a prepared statement so in my example here:

stmt, err := db.Prepare("select * from someTable where age = ? and hairColor = ?")
rows, err := stmt.Query(age,hairColor)

This seems obvious in retrospect. Hopefully this can save somebody a few minutes of hair pulling in the future!

Sign up to request clarification or add additional context in comments.

1 Comment

This doesn't seem obvious, not even in retrospect. Anyways, it should not be necessary to use Prepare just to be able to pass more than one argument. Either the original error you got is not from the query+args that you've showed, or you're using some broken/unconventional driver.

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.