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
I'm confused with your description. did it throw pq: relation "geo_commune" does not exist
? if it did, your datasource is incorrect. if it didn't, try select * from public.Geo_commune