Ros*_*own 2 postgresql insert go
在 GoLang 中对我的数据库运行任何 sql 查询时,我收到以下错误,有没有人知道我是否可以尝试解决其他任何问题
pq: relation "users" does not exist
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
检查了数据库的权限,一切正常。
检查凭据是否正常
通过 psql 在 SQL 数据库上直接运行 INSERT 语句
创建了一个极简程序以确保它不是我的代码中的任何其他内容
手动将值输入到查询字符串中,而不是使用参数
尝试 SELECT 语句和相同的问题。它只是没有找到用户表。
我已经通过 const 变量构建了连接字符串,如下所示。两者都没有工作。
我试过有和没有端口(5432)
下面的代码是我所拥有的(极简应用程序输出与我的主应用程序相同的错误)
func main() {
sqlInfo := fmt.Sprintf("postgres://postgres:postgres@localhost/user_details?sslmode=disable")
db, err := sql.Open("postgres", sqlInfo)
defer db.Close()
if err != nil {
fmt.Fprintf(os.Stdout, "Connection to the database failed")
return
}
err = db.Ping()
if err != nil {
fmt.Fprintf(os.Stdout, "Connection to the database failed")
return
}
fmt.Fprintf(os.Stdout, "You have connected to the database successfully")
sqlQuery := `INSERT INTO users ("user_id", "username", "password", "email", "gender", "gang") VALUES ($1, $2, $3, $4, $5, $6)`
_, err = db.Exec(sqlQuery, 1, "Ross8839", "rocky8839", "ross88399@hotmail.com", "Female", "Greengos")
if err != nil {
fmt.Fprintf(os.Stdout, "Failed to query db")
panic(err)
}
}
Run Code Online (Sandbox Code Playgroud)
我希望上面的代码没有错,因为我在 Windows 环境中使用了它,但我不想使用 Windows 进行开发。这一切都在我的 Linux 环境中,自从搬过来……我遇到了这个问题。
以下是 postgres 用户对数据库的权限
您的连接字符串:
postgres://postgres:postgres@localhost/user_details?sslmode=disable
Run Code Online (Sandbox Code Playgroud)
说你正在连接到user_details数据库。也许您users在不同的数据库中创建了该表。
| 归档时间: |
|
| 查看次数: |
5064 次 |
| 最近记录: |