PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, December 5, 2022

[FIXED] How to get table values by xorm from Oracle DB?

 December 05, 2022     go, go-xorm, oci8, oracle     No comments   

Issue

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


Answered By - O.Takashi
Answer Checked By - Gilberto Lyons (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing