我正在做一个 RESTful API 项目,我的代码可以用 gorm 查询,我的查询是这样的countSequenceId := db.Debug().Raw("SELECT COUNT (*) FROM SMSBlast2").Scan(&smsblast1)
。我得到了结果[1 rows affected or returned]
,这意味着成功地计算了我在数据库中的所有行,但我想显示结果result count = 10
,但是如何?
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mssql"
"log"
"net/http"
"strconv"
"time"
)
type SMSBlast struct {
SequenceID int `gorm:"primary_key";column:"SequenceID"`
MobilePhone string `gorm:"column:MobilePhone"`
Output string `gorm:"column:Output"`
WillBeSentDate *time.Time `gorm:"column:WillBeSentDate"`
SentDate *time.Time `gorm:"column:SentDate"`
Status *string `gorm:"column:Status"`
DtmUpd time.Time `gorm:"column:DtmUpd"`
}
func (SMSBlast) TableName() string {
return "SMSBlast2"
}
func allSMSBlasts(w http.ResponseWriter, r *http.Request){
db, err := gorm.Open("mssql", "sqlserver://sa:@localhost:1433?database=CONFINS")
if err != nil{
panic("failed to connect database")
}
defer db.Close()
var smsblasts []SMSBlast
db.Debug().Find(&smsblasts)
fmt.Println("{}",smsblasts)
json.NewEncoder(w).Encode(smsblasts)
}
func insertSMSBlast(w http.ResponseWriter, r *http.Request){
fmt.Println("New Insert Created")
db, err := gorm.Open("mssql", "sqlserver://sa:@localhost:1433?database=CONFINS")
if err != nil{
panic("failed to connect database")
}
defer db.Close()
vars := mux.Vars(r)
mobilephone := vars["mobilephone"]
output := vars["output"]
var(
smsblast1 SMSBlast
)
countSequenceId := db.Debug().Raw("SELECT COUNT (*) FROM SMSBlast2").Scan(&smsblast1)
fmt.Println(countSequenceId)
msg, err := json.Marshal(countSequenceId)
if err != nil{
fmt.Println(err.Error())
}
侃侃无极
相关分类