0

When I'm trying to parse data into struct and then append it into a slice, get nothing. But if I use query in MySQL workbench, I get some values....

query, err := db.Query("SELECT 'description','is_done' FROM tasks WHERE 'user_id' = ?;", userId)
if err != nil {
    return nil, err
}
defer query.Close()
var tasks []TodoUserDTO
var currentTask TodoUserDTO
for query.Next() {
    err = query.Scan(&currentTask.Description, &currentTask.IsDone)
    if err != nil {
        panic(err)
    }
    tasks = append(tasks, currentTask)
}

TodoDTO struct looks like this:

type TodoUserDTO struct {
    Description string `json:"desc"`
    IsDone      bool   `json:"done"`
}
Folium
  • 39
  • 4

1 Answers1

2

Based on the code, it looks like you're using the wrong column names in the SELECT statement of your query. The SELECT statement should include the actual column names of the columns in the tasks table, rather than the literal strings of the column names.

Try changing the SELECT statement to this:

"SELECT description, is_done FROM tasks WHERE user_id = ?"

steve 4239
  • 74
  • 2