讓我通過說我是Golang的新手,並使用Golang重構現有的基於Python的API來處理這個問題,因此數據庫和基礎架構已經存在,並且已經填充了數據。Golang API - MySQL連接但查詢爲空
我有一個非常基本的API設置使用杜松子酒和格姆。在GET API調用期間,它能夠連接到MySQL 5.7後端,但我的查詢都不會返回任何內容。我已經嘗試了數據庫中已知序列號的各種查詢,並且在我查詢此應用程序之外時正在返回。
main.go
package main
import (
"github.com/gin-gonic/gin"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"time"
"log"
"fmt"
)
var db *gorm.DB
func InitDB() *gorm.DB{
var err error
db, err := gorm.Open("mysql", "api:[email protected](10.37.1.1:3306)/opt_db1")
db.SingularTable(true)
err = db.DB().Ping()
if err != nil {
log.Panic(err)
fmt.Print(err.Error())
} else {
fmt.Printf("Successfully connected to MySQL")
}
return db
}
type TestResult struct {
ID int `db:"id" json:"id"`
DateAdded time.Time `db:"date_added" json:"date_added"`
Serial string `db:"serial" json:"serial"`
SequenceId int `db:"sequence_id" json:"sequence_id"`
LastCompletedStage int `db:"last_completed_stage" json:"last_completed_stage"`
LastCompletedSequence int `db:"last_completed_sequence" json:"last_completed_sequence"`
Workorder string `db:"workorder" json:"workorder"`
Product string `db:"product" json:"product"`
IsComplete string `db:"is_complete" json:"is_complete"`
IsScrapped string `db:"is_scrapped" json:"is_scrapped"`
ValueStream string `db:"value_stream" json:"value_stream"`
PromiseDate string `db:"promise_date" json:"promise_date"`
FailLock int `db:"fail_lock" json:"fail_lock"`
SequenceRev int `db:"sequence_rev" json:"sequence_rev"`
DateUpdated time.Time `db:"date_updated" json:"date_updated"`
Date time.Time `db:"date" json:"date"`
Time time.Time `db:"time" json:"time"`
Ptyp2 string `db:"ptyp2" json:"ptyp2"`
WoQty int `db:"wo_qty" json:"wo_qty"`
IsActive int `db:"is_active" json:"is_active"`
IsTimeLock int `db:"is_time_lock" json:"is_time_lock"`
TimeLockTimestamp time.Time `db:"time_lock_timestamp" json:"time_lock_timestamp"`
ScrapReason string `db:"scrap_reason" json:"scrap_reason"`
ScrappedBy int `db:"scrapped_by" json:"scrapped_by"`
}
func main() {
router := gin.Default()
opt := router.Group("opt/v2")
{
opt.GET("/last-completed-test/:serial", LastTest)
}
// Add API handlers here
router.Run(":3000")
}
func LastTest(c *gin.Context) {
// Connection to the database
db := InitDB()
defer db.Close()
var result TestResult
// get param and query
//serial := c.Params.ByName("serial")
db.Table("test_result").Where("serial = ?", "124111-0027").Scan(&result)
if result.ID != 0 {
//Display JSON result
c.JSON(200, result)
} else {
// Display JSON error
c.JSON(404, gin.H{"error": "Record not found", "code": 404})
}
}
控制檯輸出:
[GIN-debug] Listening and serving HTTP on :3000 Successfully connected to MySQL[GIN] 2017/02/15 - 09:05:13 |[97;43m 404 [0m| 4.5005ms | 127.0.0.1 |[97;4 4m [0m GET /opt/v2/last-completed-test/124111-0027
網絡輸出:
{"id":2252920,"date_added":"0001-01-01T00:00:00Z","serial":"","sequence_id":0,"last_completed_stage":0,"last_completed_sequence":0,"workorder":"","product":"","is_complete":"","is_scrapped":"","value_stream":"","promise_date":"","fail_lock":0,"sequence_rev":0,"date_updated":"0001-01-01T00:00:00Z","date":"0001-01-01T00:00:00Z","time":"0001-01-01T00:00:00Z","ptyp2":"","wo_qty":0,"is_active":0,"is_time_lock":0,"time_lock_timestamp":"0001-01-01T00:00:00Z","scrap_reason":"","scrapped_by":0}
架構從工作臺:
在上面的代碼我甚至硬編碼的我知道序列號有數據無濟於事。
在嘗試查詢我的數據庫時是否有某些明顯的缺失?
謝謝!
我在一個點試圖以此爲好,我做你的變化和我找回一個空的結果集。我會把JSON的回報放在我的問題上面。 – xXPhenom22Xx
我剛剛在返回的JSON中注意到一件有趣的事情,即返回的ID是正確的,字段的結果不會回來,但? – xXPhenom22Xx
TestResult結構位於頂端附近的上述代碼中。 – xXPhenom22Xx