我尝试使用xorm来获取值,并调用engine.Find(&xxxx)方法,但结果对象为空。
我检查了它是否可以通过engine.Query()方法获得结果。
这表示数据库连接正常工作。
如何将结果放入usertest01
中?
package main
import (
"database/sql"
_ "github.com/mattn/go-oci8"
"github.com/go-xorm/xorm"
)
type Usertest struct {
Id int64 `json:"id" xorm:"'id'"`
Name string `json:"name" xorm:"'name'"`
Password string `json:"password" xorm:"'password'"`
}
var engine *xorm.Engine
func main(){
engine, err = xorm.NewEngine("oci8", getDSN())
if err != nil {
fmt.Println(err.Error())
}
var usertest01 []Usertest
engine.Find(&usertest01)
fmt.Println("--- result by struct. doesn't work ---")
fmt.Println(usertest01)
usertest02, err := engine.Query("select * from usertest")
fmt.Println("--- result by Query. this works ---")
fmt.Println(usertest02)
}
控制台
[xorm] [info] 2019/04/13 18:24:45.986664 [SQL] SELECT "id", "name", "password" FROM "usertest"
--- result by struct. doesn't work---
[]
[xorm] [info] 2019/04/13 18:24:46.095214 [SQL] select * from usertest
--- result by Query. this works ---
[map[ID:[49] NAME:[78 65 77 69] PASS:[80 65 83 83]]]
答案 0 :(得分:1)
我添加了返回实际表名的方法。
type Usertest struct {
Id int64 `json:"id" xorm:"'ID'"`
Name string `json:"name" xorm:"'NAME'"`
Pass string `json:"pass" xorm:"'PASS'"`
}
// add this method
func (Usertest) TableName() string {
return "USERTEST"
}
main(){
...
...
engine.Find(&usertest01)
fmt.Println(usertest01)
}