これが私のコードです:
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)
}
}
データベース内のデータは次のとおりです。
編集: table_name:
そして私も試してみました:
db.Select(&commune, `SELECT * FROM "Geo_commune" WHERE id=1 ORDER BY name ASC`)
しかし、戻り値は常に空であり、データが存在することを確信しており、接続エラーはありません。
行がなければ"Geo_commune"
、err2 := db.Query( SELECT * FROM Geo_commune WHERE id=1 ORDER BY name ASC
) i have a : 2019/01/28 22:17:16 pq: relationship "geo_commune" does not exist
よろしく
新しいテストを編集します:
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)
}
}
私に与えます:
[]
<nil>
2019/01/29 09:50:06 pq: relation "public.geo_commune" does not exist