Fetch only ONE row QueryRow(x)?

This code fetches many rows. It works perfect AFAIK.
https://api.go4webdev.org/ques

func Get(query string) interface{} {
	// execute query to postgresql db
	if len(query) > 0 {
		var list []map[string]interface{}
		rows, err := db.Queryx(query)
		if err != nil {
			fmt.Println("no records")
		}
		defer rows.Close()
		for rows.Next() {
			row := make(map[string]interface{})
			err = rows.MapScan(row)
			if err != nil {
				log.Fatal(err)
			}
			list = append(list, row)
		}
		rows.Close()
		if len(list) == 0 {
			return ("norec")
		}
		return list
	}
	return nil
}

The question is how I accomplish the same with ONE single row? With the desired result (without brackets ):

{“que_desc”:“Sexual dysfunction, unspecified”,“que_id”:2,“que_subject”:“Robust upward-trending instruction set”}

I have tried to do this without success.

Just use QueryRow or if you are using sqlx use Get. For xample
var count int
count; err = tx.QueryRow(“SELECT count(database_id) FROM database_tr WHERE database_id = $1”, id).Scan(&count)
fmt.Println(count)

On the right track

var subject string
db.QueryRow("SELECT que_subject FROM ques WHERE que_id = $1", 2).Scan(&subject)
return (subject)

But the result is only one column and no curly brackets. And no labels.

“Robust upward-trending instruction set”

The goal is json:

{“que_desc”:“Sexual dysfunction, unspecified”,“que_id”:2,“que_subject”:“Robust upward-trending instruction set”}

I am searching after a more generic (?) solution like this pseudocode:

row := make(map[string]interface{})
db.QueryRowx(query, val).StructScan(&row)
return row

Is this possible?

This query selects a single column out and scans it into a single string field. If you instead select all the columns you need (e.g maybe SELECT * FROM ...) and then use

row := make(map[string]interface{})
db.QueryRowx(query, val).StructScan(&row)

I expect that will work.

https://github.com/jmoiron/sqlx has StructScan

I cannot get it to work. Do you mean like this?

db.QueryRowx("SELECT * FROM ques LIMIT 1")
row := make(map[string]interface{})
db.QueryRowx("SELECT * FROM ques WHERE que_id = $1", 2).StructScan(&row) 
return (row) 

returns an empty map[]

My mistake, I guess when you scan into a map you want MapScan. Try that instead of StructScan.

Thank You.

Yes and No. This works with db.Queryx but not using db.QueryRow.

(type *sqlx.Row has no field or method Next)

rows := db.Queryx(query, val)
for rows.Next() {
    results := make(map[string]interface{})
    rows.MapScan(results)
    return results
}

Is there a equivalent code for db.QueryRowx? Like:

row := make(map[string]interface{})
data := db.QueryRowx(query, val).MapScan(&row)
return (data)

Gives error:

cannot use &row (type *map[string]interface {}) as type map[string]interface {} in argument to db.QueryRowx(query, val).MapScan

The error says you can’t pass a pointer to a map as a map. Just pass the map in without taking it’s address. It’s no different than the err = rows.MapScan(row) line from your initial post, except that you’re calling it on a *sqlx.Row instead of a *sqlx.Rows.

1 Like

I do not quite follow. This gives “null” in return:

data := db.QueryRowx(query, val).MapScan(make(map[string]interface{}))
fmt.Println(data)
return (data)

And this also “null”

rows := db.QueryRowx(query, val)
results := make(map[string]interface{})
rows.MapScan(results)
return results

What am I doing wrong?

Finally I got it to work. Simple and elegant.

row := make(map[string]interface{})
db.QueryRowx(query, val).MapScan(row)
return (row)

https://api.go4webdev.org/c_que?id=2

Thank you indeed!

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