doumengmian1180 2017-12-22 21:55
浏览 597
已采纳

如何在MySQL的Golang GORM中从查询中提取数据?

I'm trying to read the data from a database with GoLang's GORM, and am new to Go in general. This is the snippet I'm trying to get work. It's to search a database using a number and check if it already exists. Just grasping at straws here, and not really understanding how GORM works, and can't find documentation that explains what to do after doing a Where Clause.

res := db.Where("Number = ?", inumber).First(&Profile{})
log.Print("Searched for profile")
if res.RecordNotFound() {
    log.Print("Record not found")
    return "", "", "", false
} else {
    log.Print("RES")

    log.Print(res.Rows())
    ret := res.Scan(&Profile{})
    return
}
return
  • 写回答

1条回答 默认 最新

  • dongwei7913 2017-12-22 22:17
    关注

    You need to retrieve data into some variable to be able to use it later:

    p := Profile{}
    res := db.Where("Number = ?", inumber).First(&p)
    
    // now use p
    log.Printf("%+v", p)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 kotlin multiplaform 的共享模块支持蓝牙吗还是说安卓和ios的蓝牙都要自己配
  • ¥15 为什么我的查询总是显示无数据,该怎么修改呢(语言-java)
  • ¥15 IDDPM采样结果异常
  • ¥100 求一个,王者荣耀查询隐藏战绩的教材,爬虫代码
  • ¥60 Cocos creator缓动问题
  • ¥15 专业问题提问,7月5号2点之前
  • ¥25 使用cube ai 导入onnx模型时报错
  • ¥15 关于#微信小程序#的问题:用一个网页显示所有关联的微信小程序数据,包括每个小程序的用户访问量
  • ¥15 root的安卓12系统上,如何使apk获得root或者高级别的系统权限?
  • ¥20 关于#matlab#的问题:如果用MATLAB函数delayseq可以对分数延时,但是延时后波形较原波形有幅度上的改变