Why the user variable isn't getting printed when the query is alright

38 views
Skip to first unread message

davetw...@gmail.com

unread,
Jun 24, 2020, 12:05:38 PM6/24/20
to golang-nuts
package main

import (
"database/sql"
"fmt"
"log"
"time"

)

type Users struct {
Id int
Username string
Password string
Email string
First_name string
Last_name string
Created_at time.Time
Super_user bool
}

func main() {
db, err := sql.Open("mysql", "root:Megamind@1@(127.0.0.1:3306)/note?parseTime=true")

if err != nil {
log.Fatalln("Couldn't connect to the database")
}

var user Users
row := db.QueryRow("select password from users where username=$1", "someone")
row.Scan(&user.Id, &user.Username, &user.Password, &user.Email, &user.First_name, &user.Last_name, &user.Created_at, &user.Super_user)
fmt.Println(user)
}




Data available in the database

mysql> select * from users; +----+---------------+--------------------------------------------------------------+---------------------------+------------+-----------+---------------------+------------+ | id | username | password | email | first_name | last_name | created_at | super_user | +----+---------------+--------------------------------------------------------------+---------------------------+------------+-----------+---------------------+------------+ | 3 | someone | $2a$10$a0g.eIGEHoVnD/s55YCePeL5BxCPYDF58nP2gb.TmYKwCuV5E7gP. | a...@gmail.com | NULL | NULL | 2020-06-22 01:24:43 | NULL | | 4 | oneanother | $2a$10$VMo4iuvruCA/yQlkfMI2QOMWo2H2jIiyyoYprKQtQMT4U7UWb78CS | z...@one.com | NULL | NULL | 2020-06-22 01:26:48 | NULL | | 5 | davetweetlive | $2a$10$oD0YKBkTvJQVPF4rilEVYemjRtwNF3ATGlVLUOVGZzR5lNx5fRl3. | p...@gmail.com | NULL | NULL | 2020-06-22 01:45:34 | NULL | +----+---------------+--------------------------------------------------------------+---------------------------+------------+-----------+---------------------+------------+ 3 rows in set (0.02 sec) mysql> exit

howar...@gmail.com

unread,
Jun 24, 2020, 12:37:58 PM6/24/20
to golang-nuts
First, please post plain text.
Second, you are not capturing the error from the query; capture and print that error and it will likely inform you what the issue is. (On the row.Scan.)
Third, perhaps the issue is your use of $1 instead of ?. 

This is regarding sqlx, but the issue they describe seems to be postgres vs. mysql issue rather than sqlx specific: https://github.com/jmoiron/sqlx/issues/78

So perhaps the error you would find after dealing with my second issue above would be something like the error the above person was getting, "Error 1054: Unknown column '$1' in 'where clause'"

Reply all
Reply to author
Forward
0 new messages