gpt4 book ai didi

postgresql - INSERT 到表中的问题

转载 作者:IT王子 更新时间:2023-10-29 01:41:08 26 4
gpt4 key购买 nike

我已经成功地建立了与 Postgres 和 GoLang 的连接,但是每当我向表中插入数据时,它都会抛出找不到表的错误

去代码

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)
}

我在控制台得到的输出是

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

我的 postgres 表名

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

postgres=#

我做错了什么??

最佳答案

你连接到数据库测试

dbname   = "test"

并且您在数据库 postgres 中有表

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

关于postgresql - INSERT 到表中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48582368/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com