Sibert
(Sibert)
June 21, 2021, 11:53am
1
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)
Sibert
(Sibert)
June 21, 2021, 2:28pm
3
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?
skillian
(Sean Killian)
June 21, 2021, 10:29pm
4
Sibert:
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.
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.
eudore
(Eudore)
June 22, 2021, 12:48am
5
Sibert
(Sibert)
June 22, 2021, 7:08am
6
skillian:
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.
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[]
skillian
(Sean Killian)
June 22, 2021, 11:10am
7
My mistake, I guess when you scan into a map you want MapScan
. Try that instead of StructScan
.
Sibert
(Sibert)
June 22, 2021, 11:36am
8
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
skillian
(Sean Killian)
June 22, 2021, 2:21pm
9
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
Sibert
(Sibert)
June 22, 2021, 2:46pm
10
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?
Sibert
(Sibert)
June 22, 2021, 2:53pm
11
skillian:
Just pass the map
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!
system
(system)
Closed
September 20, 2021, 2:54pm
12
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.