Sqlx query error

type req_demo struct {
Param1 int json:"param1" binding:"required"
Param2 int json:"param2" binding:"required"

var (
err error
data resp.data
var _req_demo req_demo // dot notation
_req_demo.Param1 = 10
_req_demo.Param2 = 20

Query := select * from demo fr where fr.param1 = :param1 and fr.param2 = :param1

err = global.Db.Select(&data, Query, jsonin)
if err != nil {
	return nil, err

I am getting "sql: converting argument $1 type: unsupported type

I am try to pass structure object to select statement.

What was the mistake in this method

I could not read your code completely. Try to format your code in Go Playground - The Go Programming Language and find “formatting errors”

But you have to put a “placeholder” in the query and then add parameters as a suffix when you send your query.

var param1, param2 string
query := `SELECT * FROM demo fr WHERE fr.param1=$1 AND fr.param2=$2
err := db.QueryRow(query, &param1, &param2).Scan(&data)
if err != nil {
   return nil, err

Thanks for your quick response.

I want pass parameter as a structure object to query.

If you use sqlx library.

I have used for updates. It is not working for select

Why? It is Postgresql that sets the rules and $1,$2 etc is the common way to pass parameters. Not only in Go. But you can Scan into a struct. But as I avoid structs for DRY reasons, I cannot guide you…

Your code example isn’t complete so it’s hard to tell. Have you tried adding DB tags to the struct? From the docs:

// Named queries can also use structs. Their bind names follow the same rules
// as the name → db mapping, so struct fields are lowercased and the db tag
// is taken into consideration.

Also that property jsonin isn’t declared anywhere in your code. Where is that coming from? Finally, I’m not familiar with sqlx but are you sure you shouldn’t be using global.Db.NamedQuery based on that comment from the docs I just referenced?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.