dqd82461 2019-02-11 07:38 采纳率: 0%
浏览 189

为什么在我使用库golang go-mssqldb连接到sql server之后,为什么打印<nil>?

I have problem , after I connection to sql server , they print <nil> , what wrong with my code or problem with my connection to sql server because they just print <nil>?

error can see in here enter image description here

package main

import (
    "database/sql"
    "fmt"
    _ "github.com/denisenkom/go-mssqldb"
    "github.com/gin-gonic/gin"
    "net/http"
)

func main()  {
    db, err :=  sql.Open("sqlserver","sqlserver://sa:@localhost:1433?database=CONFINS&connection+timeout=30")
    if err != nil{
        fmt.Print(err.Error())
    }


    err = db.Ping()

    if err != nil {
        fmt.Print(err.Error())
    }
    defer db.Close()

    type SMSBlast struct {
        SequenceID  string
        MobilePhone string
        Output  string
        WillBeSentDate string
        SentDate string
        Status string
        DtmUpd string
    }

    router := gin.Default()

    //Get a SMSBlast  detail
    router.GET("/SMSBlast2/:SequenceID", func(context *gin.Context) {
        var(
            smsblast SMSBlast
            result gin.H
        )

        SequenceID := context.Param("SequenceID")
        fmt.Println(db.Ping())
        row := db.QueryRow("select SequenceID, MobilePhone, Output, WillBeSentDate, SentDate, Status, DtmUpd from SMSBlast2 = ?;",SequenceID)
        err = row.Scan(&smsblast.SequenceID, &smsblast.MobilePhone, &smsblast.Output, &smsblast.WillBeSentDate, &smsblast.SentDate, &smsblast.Status, &smsblast.DtmUpd)
        if err != nil{
            //if no results send null
            result = gin.H{
                "result": nil,
                "count":  0,
            }
            }else{
                result = gin.H{
                    "result" : smsblast,
                    "count" : 1,
                }
            }

        context.JSON(http.StatusOK, result)
    })
    router.Run(":8080")

}
  • 写回答

2条回答

  • donglugou6652 2019-02-11 08:15
    关注
        fmt.Println(db.Ping())
    

    Prints the error returned by db.Ping(), which normally is nil.

    评论

报告相同问题?

悬赏问题

  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示