查询返回“预期0个参数,实际得到1个”。

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

query is returning "expected 0 arguments, got 1"

问题

我正在尝试查询一个我知道其中有数据的数据库,直接在pgadmin中查询时是有结果的。但是,当我使用以下代码进行查询时,却没有返回结果:

const DATABATE_URL = "postgres://postgres:pw@localhost:5432/postgresdb"

conn, err := pgx.Connect(context.Background(), DATABATE_URL)
defer conn.Close(context.Background())

if err != nil {
    fmt.Printf("Connection failed: %v\n", err)
    os.Exit(-1)
}

stmt := "SELECT * FROM nodes"
rows, err := conn.Query(context.Background(), stmt, nil)
if err != nil {
    fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) // 在这里报错 "expected 0 arguments, got 1"
    os.Exit(1)
}
for rows.Next() {
    var results string
    err = rows.Scan(&results)
    if err != nil {
        fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n %n", err)
        os.Exit(1)
    }
    fmt.Println(results)
}

当我通过 Goland 和 pgadmin 直接连接到数据库并使用相同的语句进行查询时,我可以看到所有的数据。我在这里漏掉了什么?

英文:

I am trying to query a database that I know has data in it from directly querying within pgadmin. When I query using the following code it returns no results:

const DATABATE_URL = "postgres://postgres:pw@localhost:5432/postgresdb"


conn, err := pgx.Connect(context.Background(), DATABATE_URL)
	defer conn.Close(context.Background())

	if err != nil {
		fmt.Printf("Connection failed: %v\n", err)
		os.Exit(-1)
	}

	stmt := "SELECT * FROM nodes"
	rows, err := conn.Query(context.Background(), stmt, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) //error outs here "expected 0 arguments, got 1"
		os.Exit(1)
	}
	for rows.Next() {
		var results string
		err = rows.Scan(&results)
		if err != nil {
			fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n %n", err)
			os.Exit(1)
		}
		fmt.Println(results)
	}

When I directly connect to the database through goland and pgadmin and query with the same statement I can see all the data. What am I missing here?

答案1

得分: 1

pgx的Conn.Query方法接受一个上下文、语句和参数:

func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)

你传递给它一个上下文、语句和nil

rows, err := conn.Query(context.Background(), stmt, nil)

所以nil被当作参数处理,但是你的SQL语句中没有包含任何参数占位符(例如SELECT * FROM nodes where id=$1),因此出现了错误。要修复这个问题,请运行:

rows, err := conn.Query(context.Background(), stmt)

然而,修改你的SQL语句以指定你想要的列也是值得的(例如SELECT nodename FROM nodes)。

注意:当提出这样的问题时,请将错误包含在问题正文中,而不仅仅作为代码的注释(这样很容易被忽略)。

英文:

The pgx Conn.Query accepts a context, statement and arguments:

func (c *Conn) Query(ctx context.Context, sql string, args ...interface{}) (Rows, error)

You are passing it a context, statement and nil:

rows, err := conn.Query(context.Background(), stmt, nil)

So the nil is treated as an argument but your SQL statement does not contain any argument placeholders (e.g. SELECT * FROM nodes where id=$1) hence the error. To fix this run:

rows, err := conn.Query(context.Background(), stmt)

However it would also be worth editing your sql to specify the column you want (e.g. SELECT nodename FROM nodes).

Note: When raising a question like this please include the error in the question body rather than just as a comment in the code (which is easy to miss).

huangapple
  • 本文由 发表于 2021年8月6日 03:23:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/68672454.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定