实际上,我追加后的数据库(mysql表)的最后一句话没有显示在我用Echo框架创建的Web中。
我在第39行有问题:评论项未扫描并显示在本地主机上:1324。
package main
import (
"database/sql"
"fmt"
"net/http"
_ "github.com/go-sql-driver/mysql"
"github.com/labstack/echo"
)
type datatype struct {
SiteID string `sql: "Site_ID"`
Scope string `sql: "Scope"`
Region string `sql: "Region"`
Province string `sql: "Province"`
Vendor string `sql: "Vendor"`
Supervisor string `sql: "Supervisor"`
SOAC_Date string `sql: "SOAC_Date"`
Last_Visit_Date string `sql: "Last_Visit_Date"`
Number_of_Visit string `sql: "Number_of_Visit"`
COC_Date string `sql: "COC_Date"`
FAT_Date string `sql: "FAT_Date"`
FAC_Date string `sql: "FAC_Date"`
Comment string `sql: "Comment"`
}
func pegy(c echo.Context) error {
db, err := sql.Open("mysql", "root:Abc@12345@tcp(127.0.0.1:3306)/test")
if err != nil {
return c.JSON(http.StatusBadRequest, "Error while connecting to DB!")
}
defer db.Close()
row, _ := db.Query("select Site_ID,Scope,region,province,vendor,supervisor,SOAC_Date,Last_Visit_Date,number_of_visit,COC_Date,FAT_Date,FAC_Date,Comment from test.db")
var res []string
for row.Next() {
var db datatype
row.Scan(&db.SiteID, &db.Scope, &db.Region, &db.Province, &db.Vendor, &db.Supervisor, &db.SOAC_Date, &db.Last_Visit_Date, &db.Number_of_Visit, &db.COC_Date, &db.FAT_Date, &db.FAC_Date, &db.Comment)
res = append(res, fmt.Sprintf("Site_Id: %s |Scope: %s|Region: %s |Province: %s|Vendor: %s |Supervisor: %s |SOAC_Date: %s|Last_Visit_date: %s|Number_of_Visit :%s|COC_Date: %s|FAT_Date: %s|FAC_Date: %s|Comment: %s", db.SiteID, db.Scope, db.Region, db.Province, db.Vendor, db.Supervisor, db.SOAC_Date, db.Last_Visit_Date, db.Number_of_Visit, db.COC_Date, db.FAT_Date, db.FAC_Date, db.Comment))
}
return c.JSON(http.StatusOK, res)
}
func main() {
e := echo.New() //By Shayan
e.GET("/", pegy)
e.Logger.Fatal(e.Start(":1324"))
}