如何实现2个参数,以及如何实现参数SequenceID和MobilePhone



我在实现 2 个这样的参数时遇到问题:http://localhost:8080/SMSBlast/SequenceID?SequenceID=2&MobilePhone=85261415223

如何用两列SequenceIDMobilePhone查询以获得SequenceID=2&MobilePhone=85261415223,我已经尝试了很多方法但仍然不起作用

package main
import (
    "database/sql"
    "fmt"
    _ "github.com/denisenkom/go-mssqldb"
    "github.com/gin-gonic/gin"
    "net/http"
    "time"
)
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  int
        MobilePhone string
        Output  string
        WillBeSentDate *time.Time
        SentDate *time.Time
        Status *string
        DtmUpd *time.Time
    }
    router := gin.Default()
    //Get a SMSBlast  detail
    router.POST("/SMSBlast/:SequenceID", func(context *gin.Context) {
        var(
            smsblast SMSBlast
            result gin.H
        )
    SequenceID := context.Param("SequenceID")
    MobilePhone := context.Param("MobilePhone")
        err := db.QueryRow("SELECT SequenceID,MobilePhone,Output,WillBeSentDate, SentDate, Status, DtmUpd FROM  SMSBlast2 Where SequenceID = ? AND MobilePhone = ? "+SequenceID , MobilePhone).Scan(&smsblast.SequenceID, &smsblast.MobilePhone, &smsblast.Output, &smsblast.WillBeSentDate, &smsblast.SentDate, &smsblast.Status, &smsblast.DtmUpd)
        //fmt.Println(row)
        fmt.Println(err)
        //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)
    })

您的语法不正确。试试这个:

err := db.QueryRow("SELECT SequenceID,MobilePhone,Output,WillBeSentDate, SentDate, Status, DtmUpd FROM  SMSBlast2 Where SequenceID = $1 AND MobilePhone = $2", SequenceID, MobilePhone).Scan(&smsblast.SequenceID, &smsblast.MobilePhone, &smsblast.Output, &smsblast.WillBeSentDate, &smsblast.SentDate, &smsblast.Status, &smsblast.DtmUpd)

编辑:

查询包括枚举为 $1$2 等的占位符。这些占位符用于作为参数包含的变量。在您的评论中,您将$1替换为$3.这不是它的工作原理。将其保留为 $1 ,将值存储在 SequenceID 变量中并将该变量传递给 QueryRow

下面是一个简化的示例来说明:

SequenceID := 3
err := db.QueryRow("SELECT * FROM SMSBlast2 WHERE SequenceID = $1", SequenceID).Scan(...)

现在有两个参数:

SequenceID := 3
MobilePhone := "85261415223"
err := db.QueryRow("SELECT * FROM SMSBlast2 WHERE SequenceID = $1 AND MobilePhone = $2", SequenceID, MobilePhone).Scan(...)

尝试Query(),示例如下:

SequenceID := context.Query("SequenceID")
MobilePhone := context.Query("MobilePhone")

最新更新