Ryan McDermott
Ryan McDermott

Reputation: 571

How to make an SQL query in golang with multiple values in the WHERE clause

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?

Upvotes: 4

Views: 6445

Answers (1)

Ryan McDermott
Ryan McDermott

Reputation: 571

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!

Upvotes: 3

Related Questions