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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问
  • ¥15 用MATLAB汇总拟合图
  • ¥15 智能除草机器人方案设计
  • ¥15 对接wps协作接口实现消息发送
  • ¥15 SQLite 出现“Database is locked” 如何解决?
  • ¥15 已经加了学校的隶属邮箱了,为什么还是进不去github education?😭
  • ¥15 求会做聚类,TCN的朋友有偿线上指导。以下是目前遇到的问题
  • ¥100 无网格伽辽金方法研究裂纹扩展的程序