-1

Iam writing a golang program in which i query postgres database. I want to to use $1 to supply values and should have a pattern matching

Db.Query("SELECT * FROM table where name like %$1%", user)

it says:

syntax error at or near "%"

6
  • "Some error" is not enough to define an error. Commented Apr 14, 2016 at 10:22
  • I did like this to get output Db.Query("SELECT * FROM table where name like $1", "%"+user+"%") but isn't there any better and professional way to do it Commented Apr 14, 2016 at 10:35
  • it says syntax error at or near "%" I did like this to get output Db.Query("SELECT * FROM table where name like $1","%"+user+"%") but isn't there any better and professional way to do it Commented Apr 14, 2016 at 10:41
  • 1
    which package you use ? Give us more detail about what is user... As said here stackoverflow.com/questions/25214459/go-postgresql-like-query try to escape your like request like '%$1%' Commented Apr 14, 2016 at 10:43
  • 1
    Does this answer your question? Go postgresql LIKE query Commented Sep 29, 2021 at 6:34

1 Answer 1

2

Your syntax is wrong, try

user := "%"+user+"%"

rows, err := Db.Query("SELECT * FROM table where name like $1", user)

if err!=nil{
    fmt.Println(err)
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.