在“日期$ 1”的参数化查询中出现PostgreSQL语法错误。 [英] PostgreSQL syntax error in parameterized query on "date $1"
问题描述
尝试参数化我的SQL查询(使用libpq函数 PQexecParams ),我陷入语法错误:
Trying to parameterize my SQL queries (using libpq function PQexecParams), I was stuck on a syntax error:
SELECT date $1
错误为:
ERROR: syntax error at or near "$1"
推荐答案
准备好的语句
说明为此,可以在中找到手册的其他类型的常量一章:
: :
,CAST()
和函数调用语法还可用于指定任意表达式的
运行时类型转换,如前所述在
第4.2.9节中。 为避免语法歧义,type'string'
语法
仅可用于指定简单文字的类型
'string'语法
类型的另一个限制是
不能用于数组类型;使用::
或CAST()
指定数组
常量的类型。
The
::
,CAST()
, and function-call syntaxes can also be used to specify run-time type conversions of arbitrary expressions, as discussed in Section 4.2.9. To avoid syntactic ambiguity, thetype 'string'
syntax can only be used to specify the type of a simple literal constant. Another restriction on the type'string' syntax
is that it does not work for array types; use::
orCAST()
to specify the type of an array constant.
加粗强调。
准备好的语句的参数实际上不是 sting文字,但键入的是 values ,因此您不能使用 type'string'
的形式。使用其他两种形式之一将值转换为其他类型,就像您已经发现自己一样。
Parameters for prepared statements are not actually sting literals but typed values, so you cannot use the form type 'string'
. Use one of the other two forms to cast the value to a different type, like you found yourself already.
示例:
PREPARE foo AS SELECT $1::date;
EXECUTE foo('2005-1-1');
与 PQexecParams
在libpq C库中
文档:
Similar for PQexecParams
in the libpq C library
The documentation:
...在SQL命令文本中,将显式强制转换附加到参数
符号以显示您将发送哪种数据类型。例如:
... In the SQL command text, attach an explicit cast to the parameter symbol to show what data type you will send. For example:
SELECT * FROM mytable WHERE x = $1::bigint;
这会强制将参数 $ 1
视为 bigint
,而默认情况下,
的类型与x相同。强烈建议通过这种方式或通过指定数字类型OID来强制执行参数类型
的决定。 ...
This forces parameter $1
to be treated as bigint
, whereas by default
it would be assigned the same type as x. Forcing the parameter type
decision, either this way or by specifying a numeric type OID, is
strongly recommended. ...
如上引言所述, 替代 是通过 paramTypes []
传递相应数据类型的OID-如果您确实需要强制转换。在大多数情况下,让Postgres从查询上下文派生数据类型应该很好。
The alternative, as mentioned in the quote above, is to pass the OIDs of respective data types with paramTypes[]
- if you actually need the cast. In most cases it should work just fine to let Postgres derive data types from the query context.
paramTypes []
通过OID指定要分配给参数
符号的数据类型。如果 paramTypes
为 NULL
,或者数组
中的任何特定元素为零,则服务器推断数据以与未键入文字字符串相同的方式,以
表示参数符号。
Specifies, by OID, the data types to be assigned to the parameter
symbols. If paramTypes
is NULL
, or any particular element in the array
is zero, the server infers a data type for the parameter symbol in the
same way it would do for an untyped literal string.
您可以获取OID系统目录中的数据类型 pg_type
:
You can get the OID of data types from the system catalog pg_type
:
SELECT oid FROM pg_type WHERE typname = 'date';
您必须使用正确的内部类型名称。例如: int4
表示整数
。
或方便地强制转换为 regtype
:
You must use the correct internal type name. For instance: int4
for integer
.
Or with a convenience cast to regtype
:
SELECT 'date'::regtype::oid;
这更加灵活,因为也接受类型名的已知别名。例如: int4
, int
或 integer
表示整数
。
This is more flexible as known aliases for the type name are accepted as well. For instance: int4
, int
or integer
for integer
.
这篇关于在“日期$ 1”的参数化查询中出现PostgreSQL语法错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!