sql - 为什么我不能在golang中使用多个参数我的sql查询有什么问题?
问题描述
我有多个参数的问题,我无法用 sql 查询实现 2 个参数。我仍然收到错误,错误显示 mssql:'SequenceID' 附近的语法不正确。我的查询 sql 有什么问题,或者我的代码有什么问题?
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 IS NOT NULL AND MobilePhone IS NOT NULL "+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)
})
解决方案
由于您的查询不包含任何占位符(?
字符),因此QueryRow
. 也许删除额外的参数:
db.QueryRow("select SequenceID, MobilePhone, Output, WillBeSentDate, SentDate, Status, DtmUpd from SMSblast2 where SequenceID IS NOT NULL AND MobilePhone IS NOT NULL").Scan[...]
推荐阅读
- xml - 在线性布局中动态添加多个文本视图
- eclipse - 错误:无法创建文件包/org.eclipse.equinox.p2.tests/testData/ProfilePreferencesTest
- python-3.x - 是否有一个简单的循环来滚动 6 个骰子,直到至少 4 个是相同的数字?
- spring-boot - 如何使用外部文件作为 MessageSource 的资源?
- c# - 如何从数据模板中引用视图的属性
- python - 如何在 dir(re) 中显示 regex groups() 函数?
- hyperledger-fabric - Hyperledger Fabric 安装问题 - 在尝试安装 npm 时
- ios - 当我拖动 UIImageView 然后在拖动过程中想要与显示在同一视图上的其他 imageView 进行比较
- java - 打开项目时在 Eclipse 2018-12 中找不到自动模块
- c++ - 带有标准无序映射的 SIGFPE