Search code examples
postgresqlgopattern-matchingparameterized-query

Parameter with pattern matching syntax in a PostgreSQL query


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


Solution

  • 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)
    }