登录
首页 >  Golang >  Go问答

问题:QueryRow.Scan() 无法成功解析结果?

来源:stackoverflow

时间:2024-03-24 18:18:38 367浏览 收藏

在使用 QueryRow.Scan() 检索查询结果时,开发者遇到了问题,因为 checkin_time 和 checkout_time 字段无法正确解析。检查代码后发现,错误在于连续两次记录了 tmpcheckouttime,导致 checkin_time 为空。修改代码,将第一个 println 语句中的 tmpcheckouttime 替换为 tmpcheckintime,即可解决问题。

问题内容

我有这样的行:

现在我想检索 checkin_timecheckout_time

好的,我已经得到了正确的查询。现在通过 go 代码运行查询:

package controllers

import (
    "absensi/config"
    "absensi/models"
    "encoding/json"
    "fmt"
    "net/http"
    "time"
)

func DoScanRow(w http.ResponseWriter, r *http.Request) {
    var tmpcheckintime string
    var tmpcheckouttime string
    var param models.CheckinParam
    var db = config.DBConnect()
    json.NewDecoder(r.Body).Decode(¶m)

    query1 := `SELECT checkin_time, checkout_time FROM oc_log WHERE userid=$1 AND userdate=$2;`
    row := db.QueryRow(query1, param.UserId, time.Now().Format("2006-01-02"))
    row.Scan(&tmpcheckintime, &tmpcheckouttime)

    fmt.Println("--> id: " + param.UserId)
    fmt.Println("--> date: " + time.Now().Format("2006-01-02"))
    fmt.Println("--> checkin: ", tmpcheckouttime)
    fmt.Println("--> checkout: ", tmpcheckouttime)

}

如果我通过 postman 发送此有效负载:

{ "id": "1234", "位置": "(100,100)", "距离": 4 }

我明白了:

--> 编号:1234 --> 日期:2019-10-28 --> 签入: --> 结帐:

入住和退房时间均为空。但显然只有结账时间是空的。这里出了什么问题?


解决方案


您发布的最后两行代码让您连续两次记录 tmpcheckouttime

fmt.Println("--> checkin: ", tmpcheckouttime)
    fmt.Println("--> checkout: ", tmpcheckouttime)

如果你修复了第一个 println,你会得到你所期望的结果吗?

好了,本文到此结束,带大家了解了《问题:QueryRow.Scan() 无法成功解析结果?》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>