dongnan4571 2016-10-30 10:07
浏览 50

在Golang MySQL中从数据库检索结果时出错

I am getting an error while retrieving results from database I am having a global database variable which is serving for the whole project I have a test program to test the connection and retrieve a row while i am able to connect to the database but there is an error in retrieving the row Here is my code

 //global database object for every package
 var (
     db *sql.DB
 )

 func initDatabase() bool {
    var err error
    db, err = sql.Open("mysql", "root:admin@/ipuscraper")
    if err != nil {
       fmt.Println("Error in database connection")
       return false
    }
    defer db.Close()

    err = db.Ping()
    if err != nil {
          fmt.Println("Error in database call")
          return false
    }
    var str string
    err = db.QueryRow("Select title, date, url from datesheet_ipu order by  id DESC LIMIT 1").Scan(&str)
    if err != nil {
         fmt.Println("error in returning result")
         return false
    }
    fmt.Println(str)
    return true
 }

The error is shown at db.Query() line and the error is not nil is there something wrong i am doing ?

  • 写回答

1条回答 默认 最新

  • douniangliao4327 2016-10-30 10:18
    关注

    Found the solution the problem with this was that i was scanning only one variable instead of that, i should have scanned three variables

    评论

报告相同问题?

悬赏问题

  • ¥15 vscode的问题提问
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM