英文:
Can't use gorm with "show variables" phrase
问题
我正在尝试使用gorm检索mysql变量:
sqlVars := []struct {
Name string `db:"Variable_name"`
Value int `db:"Value"`
}{}
res := sqlDB.Raw("show variables like 'max_connections'").Scan(&sqlVars)
if res.Error != nil {
log.Fatalf("failed to query MySQL parameter: %v", res.Error)
}
当我执行这个查询时,只有Value
字段被设置了,Name
字段没有被设置。相同的结构在使用sqlx
时工作正常。
有人知道是什么问题吗?
--
附上mysql输出:
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
英文:
I'm trying to retrieve mysql variables with gorm:
sqlVars = []struct {
Name string `db:"Variable_name"`
Value int `db:"Value"`
}{}
res := sqlDB.Raw("show variables like 'max_connections'").Scan(&sqlVars)
if res.Error != nil {
log.Fatalf("failed to query MySQL parameter: %v", res.Error)
}
When I execute that query, only Value
field is set, the Name
field isn't. The same struct works fine with sqlx
.
Does anyone know what's wrong?
--
Attached mysql output:
mysql> show variables like 'max_connections';
+-----------------+-------+
| Variable_name | Value |
+-----------------+-------+
| max_connections | 151 |
+-----------------+-------+
1 row in set (0.00 sec)
答案1
得分: 1
描述使用gorm
标签的字段,如https://gorm.io/docs/models.html中所述。
示例:
sqlVars := []struct {
Name string `gorm:"column:Variable_name"`
Value int `gorm:"column:Value"`
}{}
请注意,gorm
标签用于定义与数据库表列对应的字段名称。在上述示例中,Name
字段对应数据库表的Variable_name
列,Value
字段对应数据库表的Value
列。
英文:
Describe fields with gorm
tag as described in https://gorm.io/docs/models.html
Example:
sqlVars := []struct {
Name string `gorm:"column:Variable_name"`
Value int `gorm:"column:Value"`
}{}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论