Here is my code :
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq"
"log"
"time"
)
type Commune struct {
Id int `db:"id"`
Created time.Time `db:"created"`
Modified time.Time `db:"modified"`
Name string `db:"name"`
}
func main() {
var err error
db, err = sqlx.Connect("postgres", "user=toto
dbname=tata password=titi sslmode=disable")
commune := []Commune{}
db.Select(&commune, `SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
rows, err2 := db.Query(`SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
fmt.Println(commune)
fmt.Println(rows)
if err != nil {
log.Fatalln(err)
}
if err2 != nil {
log.Fatalln(err2)
}
}
here is the data in the database :
edit : the table_name :
and i've even tried with :
db.Select(&commune, `SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
but the return is always empty and i'am sure that the data exists and i don't have a connection error.
Without the "Geo_commune"
rows, err2 := db.Query(SELECT * FROM Geo_commune WHERE id=1 ORDER BY name ASC)
i have a :
2019/01/28 22:17:16 pq: relation "geo_commune" does not exist
Regards
edit my new tests :
package main
import (
"fmt"
"github.com/jmoiron/sqlx"
"log"
"time"
_ "github.com/lib/pq"
)
type Commune struct {
Id int `db:"id"`
Created time.Time `db:"created"`
Modified time.Time `db:"modified"`
Name string `db:"name"`
}
var db *sqlx.DB
func main() {
var err error
db, err = sqlx.Connect("postgres", "user=toto dbname=titi password=tata sslmode=disable")
commune := []Commune{}
if err != nil {
log.Fatalln(err)
}
db.Select(&commune, `SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
rows, err2 := db.Query(`SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
if err != nil {
log.Fatalln(err)
}
if err2 != nil {
log.Fatalln(err2)
}
db.Select(&commune, `SELECT * FROM public.Geo_commune WHERE id=1 ORDER BY name ASC`)
rows, err2 = db.Query(`SELECT * FROM public.Geo_commune WHERE id=1 ORDER BY name ASC`)
fmt.Println(commune)
fmt.Println(rows)
if err != nil {
log.Fatalln(err)
}
if err2 != nil {
log.Fatalln(err2)
}
}
gives me :
[]
<nil>
2019/01/29 09:50:06 pq: relation "public.geo_commune" does not exist


SELECT * ...? Did you try a listing the four columns that have corresponding fields in the Commune struct? This test seems to suggest thatSelectshould return an error if you're missing a "destination" field, and in the code you provided you're not checking that specific error, since the result of yourdb.Selectcall isn't assigned to anything.db.Select, why? And what's the purpose ofdb.Queryin your code? Use only one, not both.