narven
(Pedro Luz)
June 16, 2016, 7:48pm
1
I’ve been trying to work with sqlx and structs
type Account struct {
Id int `json:"id" db:"id"`
Username string `json:"username" db:"username"`
Password string `json:"password" db:"password"`
Salt string `json:"salt" db:"salt"`
Email string `json:"email" db:"email"`
}
type SignIn struct {
Password string `json:"password"`
Email string `json:"email"`
}
var signin = &SignIn{
Email: "x@gmail.com",
Password: "1234",
}
var sql string = "SELECT email, password FROM accounts WHERE email = ? LIMIT 1;"
// pass sign data into query
// put data into account struct
err := db.Get(&account, sql, signin.Email)
fmt.Println(err)
but im having a problem when passing signin.Email it never finds the record. but if i hardcode the email into the Get method… it works.
i also tried with other sqlx methods and always the same result.
any ideas?
Indeed a weird behavior. I have no idea why signin.Email fails here. In such a situation I would use either fmt.Printf("%v",…) or spew .Dump(…) to find out how signin
and signin.Email
look at runtime.
narven
(Pedro Luz)
June 16, 2016, 10:09pm
3
all the prints return the proper email as string
the spew.Dump return
(string) (len=11) "x@gmail.com"
Also I have changed my code to use the default db package and Im having the same problem:
var sql string = `
SELECT id, email, password
FROM accounts
WHERE email = ?
LIMIT 1;
`
row := db.QueryRow(sql, signIn.Email)
err = row.Scan(&account.Id, &account.Email, &account.Password)
Really dont understand the problem. Dont believe this is a bug on the libraries, probably Im doing something wrong… but already lost 3 days with this basic query.
narven
(Pedro Luz)
June 16, 2016, 10:13pm
4
right now im trying to enable the log queries on the database to try and see what is actually showing in the sql
calmh
(Jakob Borg)
June 17, 2016, 5:59am
5
Are you getting an error back on the query? Are you sure ?
is the correct place holder character for the database driver?
jhngrant
(John Grant)
June 19, 2016, 5:19pm
6
I’m using PostgreSQL and the following code prints:
main.Account{Id:0, Username:"", Password:“1234”, Salt:"", Email:"x@gmail.com "}
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"log"
)
type Account struct {
Id int `json:"id" db:"id"`
Username string `json:"username" db:"username"`
Password string `json:"password" db:"password"`
Salt string `json:"salt" db:"salt"`
Email string `json:"email" db:"email"`
}
type SignIn struct {
Password string `json:"password"`
Email string `json:"email"`
}
const dataSourceName = "host=localhost user=postgres password=ppw"
var db *sqlx.DB
func init() {
var err error
db, err = sqlx.Connect("postgres", dataSourceName)
if err != nil {
log.Fatal(err)
}
}
func main() {
fmt.Printf("%#v", get())
}
func get() Account {
sql := `
SELECT
password
, email
FROM
(VALUES (1, 'x', '1234', 'salt123', 'x@gmail.com'),
(2, 'y', '9234', 'salt923', 'y@gmail.com'))
AS accounts (id, username, password, salt, email)
WHERE email = $1;
`
signin := &SignIn{
Email: "x@gmail.com",
Password: "1234",
}
accounts := Account{}
err := db.Get(&accounts, sql, signin.Email)
if err != nil {
log.Fatal(err)
}
return accounts
}
mafredri
(Mathias Fredriksson)
June 22, 2016, 2:46pm
7
Take a look at sqlx bindvars . At least if you’re using PostgreSQL you need to use $1
, $2
, etc. instead of ?
.
system
(system)
Closed
September 20, 2016, 2:46pm
8
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.