Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

sql

go

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?

like image 838
Ryan McDermott Avatar asked Oct 20 '25 12:10

Ryan McDermott


1 Answers

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!

like image 168
Ryan McDermott Avatar answered Oct 23 '25 07:10

Ryan McDermott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!