search

Home  >  Q&A  >  body text

Golang, MySQL, unable to append query data to list of structures

<p>When I try to parse the data into a struct and then append it to the slice, I get nothing. But if I use the query in MySQL Workbench, I get some values...</p> <pre class="brush:php;toolbar:false;">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) }</pre> <p>The TodoDTO structure is as follows: </p> <pre class="brush:php;toolbar:false;">type TodoUserDTO struct { Description string `json:"desc"` IsDone bool `json:"done"` }</pre></p>
P粉135799949P粉135799949451 days ago566

reply all(1)I'll reply

  • P粉183077097

    P粉1830770972023-09-05 00:20:51

    Based on the code, it appears that you are using the wrong column name in the SELECT statement of your query. The SELECT statement should contain the actual column names of the columns in the task table, not literal strings of column names.

    Try changing the SELECT statement to:

    "Select description, is_done FROM task WHERE user_id = ?"

    reply
    0
  • Cancelreply