Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
This repository is currently being migrated. It's locked while the migration is in progress.

新手请教:刚更新了0.3.1版本,以前正常的程序现在运行期间报错了 #46

Closed
jazzthinking opened this issue Jan 8, 2014 · 8 comments

Comments

@jazzthinking
Copy link

刚更新了0.3.1版本,以前是0.2.3,以前正常的程序现在运行期间报错了,写了一个最简单的测试程序也报同样的错误,大家有遇到过吗?请指教,多谢。

最简代码:

type User struct {
    Id       int32
    UserName string
}

func main() {
  var db *xorm.Engine
  var err error
  db, err = xorm.NewEngine("mysql", fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?charset=utf8",dbUser, dbPwd, dbhost, dbName))
  if err != nil {
        fmt.Println(err)
        return
  }
  info := User{}
  _, err = db.Id(1).Get(&info)
  if err != nil {
        fmt.Println(err)
        return
  }
}

运行后报的错误为:

SELECT `User`.`Id`, `User`.`UserName`, `User`.`FullName`, `User`.`Created` FROM `User` WHERE Id=? LIMIT 1
[1]
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x0 pc=0x486584]

goroutine 1 [running]:
github.com/lunny/xorm.(*Session).bytes2Value(0xc0800b3000, 0x0, 0xc0800b7300, 0xc080000350, 0x8, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:1966 +0x124
github.com/lunny/xorm.(*Session).row2Bean(0xc0800b3000, 0xc08007d6c0, 0xc0800b8080, 0x4, 0x4, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:1637 +0x987
github.com/lunny/xorm.(*Session).Get(0xc0800b3000, 0x5d6920, 0xc08007e7c0, 0xc0800b3000, 0x0, ...)
    E:/testGo/src/github.com/lunny/xorm/session.go:922 +0x4e2
main.main()
    E:/testGo/src/sampleTest/main.go:58 +0x158

goroutine 3 [syscall]:
syscall.Syscall6(0x7746bbc0, 0x5, 0x9c, 0xc08007af00, 0xc080000250, ...)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/runtime/zsyscall_windows_windows_amd64.c:97 +0x55
syscall.GetQueuedCompletionStatus(0x9c, 0xc08007af00, 0xc080000250, 0xc080000248, 0xffffffff, ...)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/syscall/zsyscall_windows_amd64.go:507 +0x9f
net.(*resultSrv).Run(0xc080000240)
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:150 +0x151
created by net.startServer
    C:/Users/ADMINI~1/AppData/Local/Temp/2/bindist417694871/go/src/pkg/net/fd_windows.go:285 +0x10f
错误: 进程退出代码 2.

数据库:mysql 5.1.63
编译环境:windows 7 64位
GO版本:1.1.2

@jazzthinking
Copy link
Author

自己跟踪发现是下面这个函数里报的错:
func (session *Session) row2Bean(rows *sql.Rows, fields []string, fieldsCount int, bean interface{})

字段是字符串类型就会报错,
fieldType.Kind()
rawValueType.Kind()
这两个的类型不一致,导致会到 bytes2Value 里处理,然后出错。

@lunny
Copy link
Member

lunny commented Jan 8, 2014

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

2 similar comments
@lunny
Copy link
Member

lunny commented Jan 8, 2014

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

@lunny
Copy link
Member

lunny commented Jan 8, 2014

能否将你的数据库结构贴一下?我测试了你的代码是正常的。

fmt.Sprintf("%v:%v@tcp(%v:3306)/%v?  charset=utf8"

这个代码中在?和charset之间的空格应去掉。
另NewEngine返回的第二个参数err应检查是否不为nil

@jazzthinking
Copy link
Author

谢谢lunny回复,真实代码里没有没有空格,数据连接没有问题,我测过,如果只返回一个整数字段时是没有问题的,

表结构如下:
CREATE TABLE IF NOT EXISTS User (
Id bigint(20) NOT NULL AUTO_INCREMENT,
UserName varchar(255) DEFAULT NULL,
FullName varchar(255) DEFAULT NULL,
Created datetime DEFAULT NULL,
PRIMARY KEY (Id)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=3 ;

@lunny lunny closed this as completed in dd9c707 Jan 8, 2014
@jazzthinking
Copy link
Author

lunny是否理解有误,我是说,数据库的连接是正常的,如果结果集只返回一个整数字段时是没有问题的,如果有string字段时,查询依然报错。

type User struct {
Id int32
UserName string
}

@lunny
Copy link
Member

lunny commented Jan 9, 2014

再更新下看看好了没有?你用的是SameMapper?

@jazzthinking
Copy link
Author

已更新,解决了,多谢,用的是SameMapper。

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants