Golang, MySQL, unable to append query data to list of structures
P粉135799949
P粉135799949 2023-09-04 08:49:25
0
1
565
<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粉135799949
P粉135799949

reply all(1)
P粉183077097

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 = ?"

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template