Go中使用sqlBoiler进行数据绑定

huangapple go评论73阅读模式
英文:

Data binding in go using sqlBoiler

问题

以下是翻译好的内容:

我正在从MYSQL数据库中获取一些数据.. 使用查询数据是正确的(例如10行)

我想将其绑定到一个模型列表中进行显示。

但是出现了恐慌错误

type UserDetails []UserDetail
type UserDetail struct {
    id             string    `json:"id" boil:",bind"`
    ScreenName     string    `json:"screenName" boil:",bind" `
}


func (m *mysqlStore) GetUsersDetails(ctx context.Context) () {
    var userDetails []*models.UserDetail
    err := queries.Raw(`
                SELECT
            user.id,
            user.screen_name
            FROM user
    group by user.id
    `).Bind(ctx, m.db, &userDetails)
    if err != nil {
        fmt.Println(err)
    }
    fmt.Println(userDetails)
}

在这里,我使用MYSQLQuery获取了正确的数据。我想以数组列表的形式显示出来,例如:

[
 {"id":"1",
   "screenName":"test"},
 {"id":"2",
   "screenName":"test"}
]

我的Go代码有什么问题?

英文:

I am fetching some data from MYSQL database.. Using query data is getting correclty (eg 10 rows)

I want to bind into a list of model for displaying.

But panic error displaying

type UserDetails []UserDetail
type UserDetail struct {
	id             string    `json:"id" boil:",bind"`
	ScreenName     string    `json:"screenName" boil:",bind" `
}


func (m *mysqlStore) GetUsersDetails(ctx context.Context) () {
	var userDetails []*models.UserDetail
	err := queries.Raw(`
				SELECT
			user.id,
			user.screen_name
			FROM user
    group by user.id
	`).Bind(ctx, m.db, &userDetails)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(userDetails)
}

here using the MYSQLQuery i am getting the correct data. I want to display that in a list of arrary eg:

[
 {"id":"1",
   "screenName":"test"},
 {"id":"2",
   "screenName":"test"}
]

what is the issue in my go code?

答案1

得分: 0

我得到了答案

在这种情况下,结构体应该是这样的:

type UserDetail struct {
    id             string    `json:"id"`
    ScreenName     string    `json:"screenName"`  
}

以及

 var userDetails []models.UserDetail
英文:

I got the Answer

In this case struct must be

type UserDetail struct {
    id             string    `json:"id"`
    ScreenName     string    `json:"screenName"`  
}

and

 var userDetails []models.UserDetail

huangapple
  • 本文由 发表于 2021年7月21日 04:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/68461100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定