简体   繁体   English

JDBC查询抛出语法错误

[英]JDBC query throwing syntax error

Basically, what this is supposed to do is run one query, read in all the values from one column, and then run some other queries based off of those values. 基本上,这应该做的是运行一个查询,从一列中读取所有值,然后根据这些值运行其他一些查询。 It turns the values into a SQL array of integers and passes that as a parameter to the statements for the other queries, but when they execute, it throws "syntax error at or near "$1" Position: 87" 它将值转换为整数的SQL数组,并将其作为参数传递给其他查询的语句,但是当它们执行时,它将引发“在“ $ 1”或附近的语法错误”位置:87”

Example of the queries: 查询示例:

"SELECT foo.id, bar.* "
+ "FROM foo, bar y "
+ "WHERE foo.baz = bar.baz and foo.id IN ? "
+ "GROUP BY foo.id";

And the parameter-setting bit (result is an arraylist of the column's data): 和参数设置位(结果是列数据的数组列表):

    Integer[] data = result.toArray(new Integer[result.size()]);
    Array array = connection.createArrayOf("INTEGER", data);

    statement.clearParameters();
    statement.setArray(1, array);

(code is genericized because this is for homework) (代码是通用的,因为这是用于家庭作业)

The IN operator requires the use of a list of expressions put between parentheses: in (1,2,3) . IN运算符要求使用放在括号之间的表达式列表: in (1,2,3) A ? 一个? place holder is only usable for a single scalar value (a number, a string). 占位符仅可用于单个标量值(数字,字符串)。

If you want to pass the list of IDs as an array you need to change the SQL so that the place holder "accepts" an array: 如果要将ID列表作为数组传递,则需要更改SQL,以便占位符“接受”数组:

SELECT foo.id, bar.*
FROM foo
  JOIN bar y ON foo.baz = bar.baz
WHERE and foo.id = ANY (?) 
GROUP BY foo.id

Note that I also changed your old-style implicit join in the WHERE clause to the modern explicit JOIN (but that doesn't matter for your question). 请注意,我还将WHERE子句中的旧式隐式JOIN更改为现代的显式JOIN (但这与您的问题无关紧要)。

With the above syntax you can pass an array with a list of ids. 使用上述语法,您可以传递带有ID列表的数组。

The next problem you will get is that "INTEGER" isn't a valid data type in Postgres. 您将得到的下一个问题是Postgres中"INTEGER"不是有效的数据类型。 The type name is passed "as is" and data types are in lower case in Postgres. 类型名称按“原样”传递,而数据类型在Postgres中使用小写字母。 So you need to use: 因此,您需要使用:

Array array = connection.createArrayOf("integer", data);

Basically, what this is supposed to do is run one query, read in all the values from one column, and then run some other queries based off of those values 基本上,这应该做的是运行一个查询,从一列中读取所有值,然后根据这些值运行其他一些查询

Why don't you do that in a single statement? 您为什么不一口气做到这一点?

SELECT foo.id, bar.*
FROM foo
  JOIN bar y ON foo.baz = bar.baz
WHERE and foo.id IN (SELECT some_column FROM ...) --<< this is your "first query"
GROUP BY foo.id

That is almost always better than running two separate statements. 这几乎总是比运行两个单独的语句更好。


Unrelated: 无关:

The group by foo.id seems unnecessary as you don't use any aggregate functions. group by foo.id似乎是不必要的,因为您不使用任何聚合函数。 And it won't work unless foo.id is the primary key of the foo table. 除非foo.idfoo表的主键,否则它将无法正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM