实际上,我在附加后的最后一句话数据库(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"))
}