选择全部不适用于golaong gorm

时间:2016-03-23 05:14:17

标签: mysql json go crud go-gin

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

    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"`
}

这是我使用

的一个控制器
    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

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

2 个答案:

答案 0 :(得分:1)

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

  

MySQL DATE和DATETIME值的默认内部输出类型是[] byte,它允许您将值扫描到程序中的[] byte,string或sql.RawBytes变量中。

     

然而,许多人希望将MySQL DATE和DATETIME值扫描到time.Time变量中,这与MySQL中的Go to DATE和DATETIME中的逻辑相反。您可以通过使用DSN参数parseTime = true将内部输出类型从[] byte更改为time.Time来实现。您可以使用loc DSN参数设置默认的time.Time位置。

     

或者,您可以使用NullTime类型作为扫描目标,它可以同时使用time.Time和string / [] byte。

答案 1 :(得分:-1)

你真的在这里打开数据库吗?我根本没有看到真正的Open()调用......