-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 "%"

kostix
  • 47,535
  • 12
  • 80
  • 157
Hardy
  • 285
  • 1
  • 4
  • 14
  • "Some error" is not enough to define an error. – Endre Simo Apr 14 '16 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 – Hardy Apr 14 '16 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 – Hardy Apr 14 '16 at 10:41
  • 1
    which package you use ? Give us more detail about what is user... As said here http://stackoverflow.com/questions/25214459/go-postgresql-like-query try to escape your like request like '%$1%' – Manawasp Apr 14 '16 at 10:43
  • Thanks for the link Manawasp. Thats what i was lookin for exactly. – Hardy Apr 18 '16 at 07:27
  • 1
    Does this answer your question? [Go postgresql LIKE query](https://stackoverflow.com/questions/25214459/go-postgresql-like-query) – Chowta Sep 29 '21 at 06:34

1 Answers1

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)
}
p_mcp
  • 2,503
  • 5
  • 33
  • 71