Search code examples
oracle-databasegooci8go-xorm

How to get table values by xorm from Oracle DB?


I try to get values by using xorm and call engine.Find(&xxxx) method but the result object is empty. I've checked that it can get result through engine.Query() method. This means database connection is working correctly and . How to put the result in 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) 

}

console

[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]]]


Solution

  • I added the method which returns the actual table name.

    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)
    }