dongyin0628 2019-02-13 03:39
浏览 49
已采纳

如何实现2参数,以及如何通过QueryRow实现参数SequenceID和MobilePhone

I have a problem with implementing 2 parameters like this: http://localhost:8080/SMSBlast/SequenceID?SequenceID=2&MobilePhone=85261415223

How to query with two columns SequenceID and MobilePhone to get SequenceID=2&MobilePhone=85261415223, I have tried many ways but still not working

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)
    })

  • 写回答

2条回答 默认 最新

  • dongmao7195 2019-02-13 04:15
    关注

    Your syntax is incorrect. Try this:

    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)
    

    EDIT:

    The query includes placeholders enumerated as $1, $2, etc. These placeholders are for the variables you include as parameters. In your comment you replaced $1 with $3. That is not how it works. Leave it as $1, store the value in the SequenceID variable and pass that variable to QueryRow.

    Here is a simplified example to illustrate:

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

    Now with two parameters:

    SequenceID := 3
    MobilePhone := "85261415223"
    err := db.QueryRow("SELECT * FROM SMSBlast2 WHERE SequenceID = $1 AND MobilePhone = $2", SequenceID, MobilePhone).Scan(...)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度