By test test2


2019-02-11 09:54:17 8 Comments

I now work restful api , but I have problem with my error to get all data from database , where I error is cannot display json , even though my query is working in ssms , but after I Implementing in my code but not working to display json

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 all SMSBlast
    router.GET("/SMSBlast", func(context *gin.Context) {
        var(
            smsblast SMSBlast
            smsblasts []SMSBlast
        )
            rows, err :=  db.Query("select SequenceID,MobilePhone,Output,WillBeSentDate, SentDate, Status, DtmUpd from  SMSBlast2;")
            if err != nil{
                fmt.Print(err.Error())
            }
            for rows.Next(){
                err = rows.Scan(&smsblast.SequenceID, &smsblast.MobilePhone, &smsblast.Output, &smsblast.WillBeSentDate, &smsblast.SentDate, &smsblast.Status, &smsblast.DtmUpd)
                smsblasts = append(smsblasts, smsblast)
                if err != nil{
                    fmt.Print(err.Error())
                }
            }
            defer rows.Close()
            context.JSON(http.StatusOK, gin.H{
                "result" : smsblasts,
                "count"  :  len(smsblasts),
            })

    })

}

0 comments

Related Questions

Sponsored Content

3 Answered Questions

[SOLVED] Why can't I assign a *Struct to an *Interface?

  • 2012-11-22 10:55:50
  • Simon Nickerson
  • 45488 View
  • 126 Score
  • 3 Answer
  • Tags:   go

1 Answered Questions

[SOLVED] Why is json values empty

  • 2018-08-31 13:27:44
  • Chris G.
  • 39 View
  • -2 Score
  • 1 Answer
  • Tags:   go

4 Answered Questions

[SOLVED] Handling JSON Post Request in Go

  • 2013-03-28 01:16:07
  • TomJ
  • 184829 View
  • 201 Score
  • 4 Answer
  • Tags:   json go

1 Answered Questions

[SOLVED] Why the result is not as expected with flag "-race"?

  • 2018-04-26 19:41:02
  • romanitalian
  • 37 View
  • -2 Score
  • 1 Answer
  • Tags:   go

6 Answered Questions

[SOLVED] Why would I make() or new()?

  • 2012-02-16 23:42:55
  • slezica
  • 49827 View
  • 158 Score
  • 6 Answer
  • Tags:   go

2 Answered Questions

[SOLVED] Lowercase JSON key names with JSON Marshal in Go

  • 2012-07-27 18:46:19
  • ANisus
  • 31008 View
  • 141 Score
  • 2 Answer
  • Tags:   json go marshalling

1 Answered Questions

[SOLVED] Golang - retrieve multiple results from MySQL, then display them as JSON

  • 2015-10-12 03:31:38
  • asubanovsky
  • 589 View
  • -1 Score
  • 1 Answer
  • Tags:   mysql go

2 Answered Questions

[SOLVED] Decoding JSON in Golang using json.Unmarshal vs json.NewDecoder.Decode

  • 2014-01-17 22:34:55
  • Simone Carletti
  • 43949 View
  • 141 Score
  • 2 Answer
  • Tags:   json go

3 Answered Questions

[SOLVED] Converting Go struct to JSON

  • 2011-11-25 14:54:57
  • magiconair
  • 117174 View
  • 117 Score
  • 3 Answer
  • Tags:   json go

1 Answered Questions

[SOLVED] Json marshal map of structs results in empty object

  • 2016-01-14 18:01:40
  • Richard Knop
  • 493 View
  • 1 Score
  • 1 Answer
  • Tags:   go

Sponsored Content