'invalid memory address' error with go-mssql

huangapple go评论107阅读模式
英文:

'invalid memory address' error with go-mssql

问题

我遇到了一个问题,似乎无法解决,可能是因为我对GO不熟悉。我在一个服务器上有以下代码可以正常工作,但在另一个服务器上却不行。以下是代码:

// 构建数据库连接字符串,并打开数据库连接。
connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
if *debug { fmt.Printf(" connString:%s\n", connString) }
db, err = sql.Open("mssql", connString)
if err != nil { log.Fatal("Open connection failed:", err.Error()) }
err = db.Ping()
if err != nil {
fmt.Println("Cannot connect: ", err.Error())
return
}

rows, _ := db.Query( "SELECT Zip FROM Zip_Rural WHERE Zip = ?", ZipCode[0:5] )
defer rows.Close()

if !rows.Next() {
acreageRequirement = .5
}

在这行代码if !rows.Next()上,我得到了以下错误:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x20 pc=0x477918]

这段代码在另一个运行GO版本为1.4.2的服务器上可以正常工作。我有一种感觉,我在这里有一些语法错误,但不知道真正的问题是什么。在同一个文件中调用db.Exec可以正常工作,这使我相信我的数据库连接是完全正常的,但由于某种原因db.Query没有正确执行。

英文:

I'm having an issue that I can't seem to resolve, probably due to my inexperience with GO. I have the following code working on one server, but not on another. Here is the code:

  1. // Build out the connection string to the database, and then open the connection to the database.
  2. connString := fmt.Sprintf("server=%s;user id=%s;password=%s;port=%d", *server, *user, *password, *port)
  3. if *debug { fmt.Printf(" connString:%s\n", connString) }
  4. db, err = sql.Open("mssql", connString)
  5. if err != nil { log.Fatal("Open connection failed:", err.Error()) }
  6. err = db.Ping()
  7. if err != nil {
  8. fmt.Println("Cannot connect: ", err.Error())
  9. return
  10. }
  11. rows, _ := db.Query( "SELECT Zip FROM Zip_Rural WHERE Zip = ?", ZipCode[0:5] )
  12. defer rows.Close()
  13. if !rows.Next() {
  14. acreageRequirement = .5
  15. }

On the line that reads if !rows.Next() I get the following error:

  1. panic: runtime error: invalid memory address or nil pointer dereference
  2. panic: runtime error: invalid memory address or nil pointer dereference [signal 0xc0000005 code=0x0 addr=0x20 pc=0x477918]

This same code works just fine on another server, running GO version 1.4.2 on both machines. I have a feeling I just have some bad syntax somewhere in here, but have no idea what the real problem is. A call to db.Exec within the same file works just fine, which leads me to believe that my database connection is perfectly fine, but for some reason db.Query is not executing correctly.

答案1

得分: 1

db.Query可能出现错误。检查一下错误,如果不是nil,则可以假设rowsnil。也就是说,调用rows.Next()会导致段错误。如果显示错误信息,你可能会找到问题所在。

英文:

There is probably an error with db.Query. Check your error and if it is not nil, assume that rows is nil. i.e. calling rows.Next() will segfault.
If you display the error, you will probably find out what the issue is.

huangapple
  • 本文由 发表于 2015年6月20日 02:43:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/30945198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定