Connection made to the postgres server,but datas refuse to be fetched from table "students"of database "student"

huangapple go评论96阅读模式
英文:

Connection made to the postgres server,but datas refuse to be fetched from table "students"of database "student"

问题

package main

import (
	"database/sql"
	"encoding/json"
	"fmt"
	"log"
	"net/http"

	"github.com/gorilla/mux"
	_ "github.com/lib/pq"
)

func logFatal(err error) {
	if err != nil {
		log.Fatal(err)
	}
}

const (
	HOST     = "localhost"
	PORT     = 5432
	USER     = "Blablabla"
	PASSWORD = ""
	DBNAME   = "student"
)

type student struct {
	ID          int    `json:"id"`
	Name        string `json:"name"`
	Department  string `json:"department"`
	Year_Joined string `json:"year_joined"`
}

var students []student

var db *sql.DB

func main() {

	psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
		"password=%s dbname=%s sslmode=disable",
		HOST, PORT, USER, PASSWORD, DBNAME)

	var err error

	db, err = sql.Open("postgres", psqlInfo)
	logFatal(err)

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Connected")

	router := mux.NewRouter()
	router.HandleFunc("/students", getStudents).Methods("GET")

	log.Fatal(http.ListenAndServe(":8000", router))

}

func getStudents(w http.ResponseWriter, r *http.Request) {
	var theStudent student

	students = []student{}

	rows, err := db.Query("select * from students")

	if err != nil {
		fmt.Print("there was an error", err)
	}

	for rows.Next() {
		err := rows.Scan(&theStudent.ID, &theStudent.Name, &theStudent.Department, &theStudent.Year_Joined)

		if err != nil {
			log.Fatal(err)
		}
		students = append(students, theStudent)

	}

	json.NewEncoder(w).Encode(students)

}

这是你要翻译的代码部分。

英文:
package main
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
_ "github.com/lib/pq"
)
func logFatal(err error) {
if err != nil {
log.Fatal(err)
}
}
const (
HOST     = "localhost"
PORT     = 5432
USER     = "Blablabla"
PASSWORD = ""
DBNAME   = "student"
)
type student struct {
ID          int    `json:"id"`
Name        string `json:"name"`
Department  string `json:"department"`
Year_Joined string `json:"year_joined"`
}
var students []student
var db *sql.DB
func main() {
psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
"password=%s dbname=%s sslmode=disable",
HOST, PORT, USER, PASSWORD, DBNAME)
var err error
db, err = sql.Open("postgres", psqlInfo)
logFatal(err)
err = db.Ping()
if err != nil {
log.Fatal(err)
}
fmt.Println("Connected")
router := mux.NewRouter()
router.HandleFunc("/students", getStudents).Methods("GET")
log.Fatal(http.ListenAndServe(":8000", router))
}
func getStudents(w http.ResponseWriter, r *http.Request) {
var theStudent student
students = []student{}
rows, err := db.Query("select * from students")
if err != nil {
fmt.Print("there was an error", err)
}
for rows.Next() {
err := rows.Scan(&theStudent.ID, &theStudent.Name, &theStudent.Department, &theStudent.Year_Joined)
if err != nil {
log.Fatal(err)
}
students = append(students, theStudent)
}
json.NewEncoder(w).Encode(students)
}

And this is the database

Last login: Sat May 29 07:24:12 on ttys003
/Applications/Postgres.app/Contents/Versions/11/bin/psql -p5432 "student"
~   new-feature ●✚  /Applications/Postgres.app/Contents/Versions/11/bin/psql -p5432 "student"
psql (11.2)
Type "help" for help.
student=# select * from students;
id | name  | department | year_joined 
----+-------+------------+-------------
1 | ola   | csc        | 1990
2 | Kenny | math       | 2019
(2 rows)
student=# 

And this is the result am getting from vscode terminal

Connected
there was an error pq: relation "students" does not exist<nil>`enter code here`

答案1

得分: 1

经过逐行调试,我意识到与数据库的连接是正常的,但问题出在 PostgreSQL 的语法上,语法应该是 'selectfrom from table' 而不是 'select * from table'。这意味着它不支持在 select 和 () 之间以及 (*) from from 之间有空格。谢谢。

英文:

After line by line debugging ,i realised that connection to the database worked but the problem is at the postgresql syntax,the syntax supposed to be 'selectfrom from table' instead of 'select * from table'.Which means it does not support space between the select and () and (*) from from.Thanks

huangapple
  • 本文由 发表于 2021年5月29日 15:52:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/67749029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定