I'm building a golang model and using gorm to query mysql database. When I do the select query with gorm, I use this:
rows,err := db.Table(my_table_name).Where(my_condition).Select(fields_select).Rows
I espect that it will return for me the rows (type *sql.rows) but the notify says 0 rows affected or returned, I have log out the query:
SELECT id, post_data FROM `post_data` WHERE (id=57)
I then pass this query to mysql, it return value, but when run in go it's not. So what is the problem please help
Here my full function select
func (m MY_Model) Select(condition map[string]string, table_name string, fields string) *sql.Rows
{
// Here I create a condition string
str_condition := ""
for k,v := range condition {
str_condition += k + "=" + v + " AND "
}
last:=len(str_condition)-5;
str_condition = str_condition[:last]
// Here I create a My_Model object to connect to database
mm := MY_Model{}
mm.DB = mm.Connect_database()
rows, err := mm.DB.Debug().Table(table_name).Where(str_condition).Select(fields).Rows()
if err != nil {
fmt.Println(err)
}
defer rows.Close()
mm.Close_database(mm.DB)
return rows
}
Here is my MY_Model struct
type mysql struct
{
Mysql mysql_config `json:"MySQL"`
}
type mysql_config struct
{
Host string `json:"Host"`
Port string `json:"Port"`
DBName string `json:"DBName"`
Username string `json:"Username"`
Password string `json:"Password"`
}