gpt4 book ai didi

mysql - 全选在 golaong gorm 中不起作用

转载 作者:数据小太阳 更新时间:2023-10-29 03:27:31 26 4
gpt4 key购买 nike

我正在使用 gin 框架并尝试使用 grom 进行 crud 操作。我正在尝试从 MYSQL 数据库中获取数据。我有 db.go 来获取数据库实例,每个表和模型的一些 Controller 我有一个这样的模型

    type Campaigns struct {

ID int `json:"id" form:"id" gorm:"column:CampaignID"`
UserID int `json:"userId" form:"userId" gorm:"column:UserID"`
Name string `json:"name" form:"name" gorm:"column:Name"`
StartDate time.Time `json:"start" form:"start" gorm:"column:StartDate"`
EndDate time.Time `json:"end" form:"end" gorm:"column:EndDate"`
Customer string `json:"customer" form:"customer" gorm:"column:Customer"`
CustomerID int `json:"customerId" form:"customerId" gorm:"column:CustomerID"`
ImpressionsCounter int `json:"ImpressionsCounter" form:"ImpressionsCounter" gorm:"column:ImpressionsCounter"`
MaxImpressions int `json:"maxImpressions" form:"maxImpressions" gorm:"column:MaxImpressions"`
CurrentSpend float64 `json:"currentSpend" gorm:"column:CurrentSpend"`
MaxSpend float64 `json:"maxSpend" form:"maxSpend" gorm:"column:MaxSpend"`
Active bool `json:"active" form:"active" gorm:"column:Active"`
Created time.Time `json:"created" gorm:"column:DateCreated"`
Updated time.Time `json:"updated" gorm:"column:DateCreated"`
}

这是我正在使用的一个 Controller

    package controllers
import (
"time"

"github.com/op/go-logging"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"

"../models"
)

var log = logging.MustGetLogger("AsAPI")

type AsController struct {
DB gorm.DB
}

func (ac *AsController) SetDB(d gorm.DB) {
ac.DB = d
ac.DB.LogMode(true)
}


// Get all table
func (ac *AsController) ListTable(c *gin.Context) {

var results []models.Campaigns
err := ac.DB.Find(&results)

if err != nil {
log.Debugf("Error when looking up Table, the error is '%v'", err)
res := gin.H{
"status": "404",
"error": "No Table found",
}
c.JSON(404, res)
return
}
content := gin.H{
"status": "200",
"result": "Success",
"Table": results,
}

c.Writer.Header().Set("Content-Type", "application/json")
c.JSON(200, content)
}

获取数据库连接我正在使用

package controllers
import (
"time"

"github.com/op/go-logging"
"github.com/gin-gonic/gin"
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"

"../models"
)

var log = logging.MustGetLogger("AdsAPI")

type AsController struct {
DB gorm.DB
}

func (ac *AsController) SetDB(d gorm.DB) {
ac.DB = d
ac.DB.LogMode(true)
}

我正在使用以下路线

ac := controllers.AdsController{}
ac.SetDB(dc.GetDB())


// Get a Ads resource
router := gin.Default()

router.GET("/table", ac.ListTables)

当我运行这个时,我得到以下错误

(/api/controllers/table.go:30) 
[2016-03-23 09:56:39] [0.99ms] SELECT * FROM `tables`
2016/03/23 09:56:39 Error when looking up tables, the error is '&{0xc8202140e0 sql: Scan error on column index 3: unsupported driver -> Scan pair: []uint8 -> *time.Time 1 <nil> 0xc82022f860 0xc82022f7c0 0xc82021e140 2 {0xc8201fb4a0} <nil> false map[] map[]}'
[GIN] 2016/03/23 - 09:56:39 | 404 | 1.153811ms | 127.0.0.1 | GET /table

这个错误的原因是什么?帮我解决这个错误?

最佳答案

你可以在驱动文档中找到答案 https://github.com/go-sql-driver/mysql#timetime-support :

The default internal output type of MySQL DATE and DATETIME values is []byte which allows you to scan the value into a []byte, string or sql.RawBytes variable in your programm.

However, many want to scan MySQL DATE and DATETIME values into time.Time variables, which is the logical opposite in Go to DATE and DATETIME in MySQL. You can do that by changing the internal output type from []byte to time.Time with the DSN parameter parseTime=true. You can set the default time.Time location with the loc DSN parameter.

Alternatively you can use the NullTime type as the scan destination, which works with both time.Time and string / []byte.

关于mysql - 全选在 golaong gorm 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36170330/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com