How to properly bind postgresql variable in Go?









up vote
0
down vote

favorite












I have this function that is returning an error sql: Rows are closed. I can't figure out why...



Here is the function :



func GetUserFromToken(db *sql.DB, token string) User 
query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token=$1
`

rows, err := db.Query(query, token)
if err != nil
fmt.Println("query error : " + err.Error())


var user User
rows.Next()
err = rows.Scan(&user.ID, &user.Token, &user.Name, &user.Surname, &user.Phone, &user.Email)
if err != nil
fmt.Println("scan error : " + err.Error())


return user



When I log the token it's the proper token. When I hard code the token in the query for testing purposes, it works fine. For instance :



query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token='abcdefg12345'
`


Tried also to set the query such as :



query := "SELECT id, token, name, surname, phone, email FROM users WHERE token = $1"

row := db.QueryRow(query, "abcdefg12345")


It works fine. fmt.Println(token) prints abcdefg12345.



Would someone help me understand what I'm missing ?




UPDATE : found my fail.



So the token I had was the bearer token extracted from the header with the following function :



func GetBearerToken(r *http.Request) string 
reqToken := r.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer")
reqToken = splitToken[1]
return reqToken



Had a leading whitespace that I did not notice in my fmt.Println. After a good night of sleep thinking about @RayfenWindspear's comment I had this urging to check string length then I saw the fail. Feeling a bit idiotic and amused at the same time that I didn't catch it.



So my simple fix :
from: reqToken = splitToken[1]
to : strings.TrimSpace(splitToken[1])










share|improve this question























  • If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
    – RayfenWindspear
    Nov 8 at 22:09











  • Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
    – Ado Ren
    Nov 8 at 22:24







  • 1




    Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
    – RayfenWindspear
    Nov 8 at 22:53











  • Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
    – Ado Ren
    Nov 9 at 8:03














up vote
0
down vote

favorite












I have this function that is returning an error sql: Rows are closed. I can't figure out why...



Here is the function :



func GetUserFromToken(db *sql.DB, token string) User 
query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token=$1
`

rows, err := db.Query(query, token)
if err != nil
fmt.Println("query error : " + err.Error())


var user User
rows.Next()
err = rows.Scan(&user.ID, &user.Token, &user.Name, &user.Surname, &user.Phone, &user.Email)
if err != nil
fmt.Println("scan error : " + err.Error())


return user



When I log the token it's the proper token. When I hard code the token in the query for testing purposes, it works fine. For instance :



query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token='abcdefg12345'
`


Tried also to set the query such as :



query := "SELECT id, token, name, surname, phone, email FROM users WHERE token = $1"

row := db.QueryRow(query, "abcdefg12345")


It works fine. fmt.Println(token) prints abcdefg12345.



Would someone help me understand what I'm missing ?




UPDATE : found my fail.



So the token I had was the bearer token extracted from the header with the following function :



func GetBearerToken(r *http.Request) string 
reqToken := r.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer")
reqToken = splitToken[1]
return reqToken



Had a leading whitespace that I did not notice in my fmt.Println. After a good night of sleep thinking about @RayfenWindspear's comment I had this urging to check string length then I saw the fail. Feeling a bit idiotic and amused at the same time that I didn't catch it.



So my simple fix :
from: reqToken = splitToken[1]
to : strings.TrimSpace(splitToken[1])










share|improve this question























  • If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
    – RayfenWindspear
    Nov 8 at 22:09











  • Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
    – Ado Ren
    Nov 8 at 22:24







  • 1




    Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
    – RayfenWindspear
    Nov 8 at 22:53











  • Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
    – Ado Ren
    Nov 9 at 8:03












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have this function that is returning an error sql: Rows are closed. I can't figure out why...



Here is the function :



func GetUserFromToken(db *sql.DB, token string) User 
query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token=$1
`

rows, err := db.Query(query, token)
if err != nil
fmt.Println("query error : " + err.Error())


var user User
rows.Next()
err = rows.Scan(&user.ID, &user.Token, &user.Name, &user.Surname, &user.Phone, &user.Email)
if err != nil
fmt.Println("scan error : " + err.Error())


return user



When I log the token it's the proper token. When I hard code the token in the query for testing purposes, it works fine. For instance :



query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token='abcdefg12345'
`


Tried also to set the query such as :



query := "SELECT id, token, name, surname, phone, email FROM users WHERE token = $1"

row := db.QueryRow(query, "abcdefg12345")


It works fine. fmt.Println(token) prints abcdefg12345.



Would someone help me understand what I'm missing ?




UPDATE : found my fail.



So the token I had was the bearer token extracted from the header with the following function :



func GetBearerToken(r *http.Request) string 
reqToken := r.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer")
reqToken = splitToken[1]
return reqToken



Had a leading whitespace that I did not notice in my fmt.Println. After a good night of sleep thinking about @RayfenWindspear's comment I had this urging to check string length then I saw the fail. Feeling a bit idiotic and amused at the same time that I didn't catch it.



So my simple fix :
from: reqToken = splitToken[1]
to : strings.TrimSpace(splitToken[1])










share|improve this question















I have this function that is returning an error sql: Rows are closed. I can't figure out why...



Here is the function :



func GetUserFromToken(db *sql.DB, token string) User 
query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token=$1
`

rows, err := db.Query(query, token)
if err != nil
fmt.Println("query error : " + err.Error())


var user User
rows.Next()
err = rows.Scan(&user.ID, &user.Token, &user.Name, &user.Surname, &user.Phone, &user.Email)
if err != nil
fmt.Println("scan error : " + err.Error())


return user



When I log the token it's the proper token. When I hard code the token in the query for testing purposes, it works fine. For instance :



query := `
SELECT id, token, name, surname, phone, email FROM users WHERE token='abcdefg12345'
`


Tried also to set the query such as :



query := "SELECT id, token, name, surname, phone, email FROM users WHERE token = $1"

row := db.QueryRow(query, "abcdefg12345")


It works fine. fmt.Println(token) prints abcdefg12345.



Would someone help me understand what I'm missing ?




UPDATE : found my fail.



So the token I had was the bearer token extracted from the header with the following function :



func GetBearerToken(r *http.Request) string 
reqToken := r.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer")
reqToken = splitToken[1]
return reqToken



Had a leading whitespace that I did not notice in my fmt.Println. After a good night of sleep thinking about @RayfenWindspear's comment I had this urging to check string length then I saw the fail. Feeling a bit idiotic and amused at the same time that I didn't catch it.



So my simple fix :
from: reqToken = splitToken[1]
to : strings.TrimSpace(splitToken[1])







postgresql go






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 8:06

























asked Nov 8 at 22:06









Ado Ren

9518




9518











  • If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
    – RayfenWindspear
    Nov 8 at 22:09











  • Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
    – Ado Ren
    Nov 8 at 22:24







  • 1




    Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
    – RayfenWindspear
    Nov 8 at 22:53











  • Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
    – Ado Ren
    Nov 9 at 8:03
















  • If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
    – RayfenWindspear
    Nov 8 at 22:09











  • Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
    – Ado Ren
    Nov 8 at 22:24







  • 1




    Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
    – RayfenWindspear
    Nov 8 at 22:53











  • Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
    – Ado Ren
    Nov 9 at 8:03















If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
– RayfenWindspear
Nov 8 at 22:09





If you are only looking for a single row, perhaps you should be using golang.org/pkg/database/sql/#DB.QueryRow which wouldn't give you this issue as a *sql.Row doesn't need to be closed. Also note that rows.Next() returns an error you aren't bothering to check.
– RayfenWindspear
Nov 8 at 22:09













Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
– Ado Ren
Nov 8 at 22:24





Ok switched to QueryRow. I now face sql: no rows in result set. I don't quite understand since the token passed is the relevant one (I checked by adding a fmt.Println(token) at start).
– Ado Ren
Nov 8 at 22:24





1




1




Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
– RayfenWindspear
Nov 8 at 22:53





Odd. You might want to take a look at what charset your postgres instance is using as well as what charset could possibly be coming from user input (or wherever this function is being called from). Aside from that, I leave it to the rest of the community... :(
– RayfenWindspear
Nov 8 at 22:53













Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
– Ado Ren
Nov 9 at 8:03




Thanks RayfenWindspear I updated my post. Your comment lead me in the proper direction!
– Ado Ren
Nov 9 at 8:03












1 Answer
1






active

oldest

votes

















up vote
2
down vote



accepted










Here's how you'd write this code. The err returned will be sql.ErrNoRows if nothing was returned. Let me know how this works, and then maybe I can give other debugging steps.



func GetUserFromToken(db *sql.DB, token string) (u User, err error) 
err = db.QueryRow(
"SELECT id, token, name, surname, phone, email FROM users WHERE token=$1",
token,
).Scan(
&u.ID,
&u.Token,
&u.Name,
&u.Surname,
&u.Phone,
&u.Email)
return






share|improve this answer




















  • Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
    – Ado Ren
    Nov 9 at 8:02











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216869%2fhow-to-properly-bind-postgresql-variable-in-go%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
2
down vote



accepted










Here's how you'd write this code. The err returned will be sql.ErrNoRows if nothing was returned. Let me know how this works, and then maybe I can give other debugging steps.



func GetUserFromToken(db *sql.DB, token string) (u User, err error) 
err = db.QueryRow(
"SELECT id, token, name, surname, phone, email FROM users WHERE token=$1",
token,
).Scan(
&u.ID,
&u.Token,
&u.Name,
&u.Surname,
&u.Phone,
&u.Email)
return






share|improve this answer




















  • Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
    – Ado Ren
    Nov 9 at 8:02















up vote
2
down vote



accepted










Here's how you'd write this code. The err returned will be sql.ErrNoRows if nothing was returned. Let me know how this works, and then maybe I can give other debugging steps.



func GetUserFromToken(db *sql.DB, token string) (u User, err error) 
err = db.QueryRow(
"SELECT id, token, name, surname, phone, email FROM users WHERE token=$1",
token,
).Scan(
&u.ID,
&u.Token,
&u.Name,
&u.Surname,
&u.Phone,
&u.Email)
return






share|improve this answer




















  • Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
    – Ado Ren
    Nov 9 at 8:02













up vote
2
down vote



accepted







up vote
2
down vote



accepted






Here's how you'd write this code. The err returned will be sql.ErrNoRows if nothing was returned. Let me know how this works, and then maybe I can give other debugging steps.



func GetUserFromToken(db *sql.DB, token string) (u User, err error) 
err = db.QueryRow(
"SELECT id, token, name, surname, phone, email FROM users WHERE token=$1",
token,
).Scan(
&u.ID,
&u.Token,
&u.Name,
&u.Surname,
&u.Phone,
&u.Email)
return






share|improve this answer












Here's how you'd write this code. The err returned will be sql.ErrNoRows if nothing was returned. Let me know how this works, and then maybe I can give other debugging steps.



func GetUserFromToken(db *sql.DB, token string) (u User, err error) 
err = db.QueryRow(
"SELECT id, token, name, surname, phone, email FROM users WHERE token=$1",
token,
).Scan(
&u.ID,
&u.Token,
&u.Name,
&u.Surname,
&u.Phone,
&u.Email)
return







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 8 at 23:15









Alex Guerra

1,678815




1,678815











  • Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
    – Ado Ren
    Nov 9 at 8:02

















  • Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
    – Ado Ren
    Nov 9 at 8:02
















Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
– Ado Ren
Nov 9 at 8:02





Thanks for your answer, even tho it is not what helped fixed the issue it's much more clean and I can return an error. I edited my question in which I explain what was my mistake.
– Ado Ren
Nov 9 at 8:02


















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216869%2fhow-to-properly-bind-postgresql-variable-in-go%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế