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

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

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

问题

  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "github.com/gorilla/mux"
  9. _ "github.com/lib/pq"
  10. )
  11. func logFatal(err error) {
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. }
  16. const (
  17. HOST = "localhost"
  18. PORT = 5432
  19. USER = "Blablabla"
  20. PASSWORD = ""
  21. DBNAME = "student"
  22. )
  23. type student struct {
  24. ID int `json:"id"`
  25. Name string `json:"name"`
  26. Department string `json:"department"`
  27. Year_Joined string `json:"year_joined"`
  28. }
  29. var students []student
  30. var db *sql.DB
  31. func main() {
  32. psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
  33. "password=%s dbname=%s sslmode=disable",
  34. HOST, PORT, USER, PASSWORD, DBNAME)
  35. var err error
  36. db, err = sql.Open("postgres", psqlInfo)
  37. logFatal(err)
  38. err = db.Ping()
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. fmt.Println("Connected")
  43. router := mux.NewRouter()
  44. router.HandleFunc("/students", getStudents).Methods("GET")
  45. log.Fatal(http.ListenAndServe(":8000", router))
  46. }
  47. func getStudents(w http.ResponseWriter, r *http.Request) {
  48. var theStudent student
  49. students = []student{}
  50. rows, err := db.Query("select * from students")
  51. if err != nil {
  52. fmt.Print("there was an error", err)
  53. }
  54. for rows.Next() {
  55. err := rows.Scan(&theStudent.ID, &theStudent.Name, &theStudent.Department, &theStudent.Year_Joined)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. students = append(students, theStudent)
  60. }
  61. json.NewEncoder(w).Encode(students)
  62. }

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

英文:
  1. package main
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "net/http"
  8. "github.com/gorilla/mux"
  9. _ "github.com/lib/pq"
  10. )
  11. func logFatal(err error) {
  12. if err != nil {
  13. log.Fatal(err)
  14. }
  15. }
  16. const (
  17. HOST = "localhost"
  18. PORT = 5432
  19. USER = "Blablabla"
  20. PASSWORD = ""
  21. DBNAME = "student"
  22. )
  23. type student struct {
  24. ID int `json:"id"`
  25. Name string `json:"name"`
  26. Department string `json:"department"`
  27. Year_Joined string `json:"year_joined"`
  28. }
  29. var students []student
  30. var db *sql.DB
  31. func main() {
  32. psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
  33. "password=%s dbname=%s sslmode=disable",
  34. HOST, PORT, USER, PASSWORD, DBNAME)
  35. var err error
  36. db, err = sql.Open("postgres", psqlInfo)
  37. logFatal(err)
  38. err = db.Ping()
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. fmt.Println("Connected")
  43. router := mux.NewRouter()
  44. router.HandleFunc("/students", getStudents).Methods("GET")
  45. log.Fatal(http.ListenAndServe(":8000", router))
  46. }
  47. func getStudents(w http.ResponseWriter, r *http.Request) {
  48. var theStudent student
  49. students = []student{}
  50. rows, err := db.Query("select * from students")
  51. if err != nil {
  52. fmt.Print("there was an error", err)
  53. }
  54. for rows.Next() {
  55. err := rows.Scan(&theStudent.ID, &theStudent.Name, &theStudent.Department, &theStudent.Year_Joined)
  56. if err != nil {
  57. log.Fatal(err)
  58. }
  59. students = append(students, theStudent)
  60. }
  61. json.NewEncoder(w).Encode(students)
  62. }

And this is the database

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

And this is the result am getting from vscode terminal

  1. Connected
  2. 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:

确定