jackc / pgx

PostgreSQL driver and toolkit for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

LIKE: could not determine data type of parameter $1

wassimbj opened this issue · comments

Describe the bug
The SQL LIKE operation doesn't seem to work with prepared statements, i tried casting and it didn't work too

just to give a more details about the error, i printed the SQL statement i'm executing:

SELECT field1, field2, field3
 FROM tbl
 WHERE field1 LIKE '%$1::text%' AND field2 > $2
 ORDER BY field3 DESC
 LIMIT $3

As i said i tried casting as the example above and it didn't work

To Reproduce
Steps to reproduce the behavior:
Just a simple LIKE SQL query to search for anything

If possible, please provide runnable example such as:

package main

import (
	"context"
	"log"
	"os"
        "github.com/georgysavva/scany/pgxscan"
	"github.com/jackc/pgx/v5"
)

func main() {
	conn, err := pgx.Connect(context.Background(), os.Getenv("DATABASE_URL"))
	if err != nil {
		log.Fatal(err)
	}
	defer conn.Close(context.Background())

	// Your code here...
       sql_stmt := "SELECT * FROM users where firstname LIKE '%$1" + "%'"
       param_value := "wassim"
       var data []interface{}
       err := pgxscan.Select(context.Background(), conn, &data, sql_stmt, param_val)
      fmt.Println(err)
}

Expected behavior
Expected to get my data back

Actual behavior
An error that says: could not determine data type of parameter $1

Version

  • Go: $ go version -> [go version go1.17 linux/amd64]
  • PostgreSQL: $ psql --no-psqlrc --tuples-only -c 'select version()' -> [PostgreSQL 13.11 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit]
  • pgx: $ grep 'github.com/jackc/pgx/v[0-9]' go.mod -> [v4.13.0]

Additional context
I'm running the database and the server inside docker containers

Just a quick note, i upgraded scany and pgx to the latest version and i still have this issue

The SQL is incorrect. Bound parameters cannot be inside of string literals.

This would work:

sql_stmt := "SELECT * FROM users where firstname LIKE $1"
param_value := "%wassim%"