dreamer2014520 2018-02-02 12:08 采纳率: 0%
浏览 137
已采纳

INSERT插入表的问题

I have successfully established connection with Postgres and GoLang but whenever I insert data in to the table it throws an error of not finding the table

Go Code

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = "root"
    dbname   = "test"
)

func main() {
    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()
    err = db.Ping()
    if err != nil {
        panic(err)
    }
    fmt.Println("Connection Success")

    sqlStatement := `
INSERT INTO users (id, age, first_name, last_name, email)  
VALUES ($1, $2, $3, $4, $5)  
RETURNING id`
    id := 0
    err = db.QueryRow(sqlStatement, 30, 26, "firstname", "lastname", "Calhoun@mail.com").Scan(&id)
    if err != nil {
        panic(err)
    } 

The ouput I get in console is

yashkumar@atri-HP-15-Notebook-PC:~/Documents/Feb2.18/src$ go run dbin.go
Connection Success
panic: pq: relation "users" does not exist

goroutine 1 [running]:
main.main()
        /home/yashkumar/Documents/Feb2.18/src/dbin.go:40 +0x6be
exit status 2

My postgres table name

postgres=# \dt
         List of relations
 Schema | Name  | Type  |  Owner   
--------+-------+-------+----------
 public | users | table | postgres
(1 row)

postgres=# 

What am I doing wrong??

  • 写回答

1条回答 默认 最新

  • dongxie3681 2018-02-02 12:18
    关注

    you connect to database test

    dbname   = "test"
    

    and you have table in database postgres

    postgres=# \dt
             List of relations
     Schema | Name  | Type  |  Owner   
    --------+-------+-------+----------
     public | users | table | postgres
    (1 row)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求数学坐标画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站