如何在预编译语句中使用sql函数

时间:2017-06-26 15:29:04

标签: php postgresql prepared-statement sql-function

我尝试在Postgres / php中向一个反域添加1 但似乎不适合使用参数

的查询

我的查询是:

UPDATE calendar."event" SET "sequence"=$1,"id_calendar"=$2,"starttime"=$3,"endtime"=$4,"summary"=$5,"description"=$6,"remark"=$7,"id_address"=$8,"available"=$9,"changed"=$10,"auth_user"=$11 WHERE ("id" = 23)

带参数的数组:

'sequence' => 'sequence+1',
'id_calendar' => '1',
'starttime' => '2017-06-28 11:59:00',
'endtime' => '2017-06-28 18:00:00',
'summary' => 'test',
'description' => '',
'remark' => '',
'id_address' => '25',
'available' => '1',
'changed' => 'now()',
'auth_user' => '5',

列的功能已更改'工作得很好。
但是'序列的总和'是错误的。

'ERROR: 22P02: invalid input syntax for integer: "sequence+1"

我尝试过额外的引号或添加像abs()这样的函数。但没有任何帮助 谷歌也无法帮助。

准备和执行我使用的查询:
  - pg_send_prepare()
  - pg_send_execute()

我做错了什么? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

您无法绑定函数调用,但无需绑定require 'socket' s = UDPSocket.new s.bind('127.0.0.1', 8100) while true do begin line = s.recvfrom_nonblock(65536)[0] puts line if line.include?('test1') || line.include?('test2') || line.include?('somestring') rescue IO::WaitReadable IO.select([s]) retry end end 。您只需要绑定变量(动态)。只需将now()直接放在语句中即可。

答案 1 :(得分:1)

我相信,使用上面列出的语法,您尝试使用值' sequence + 1'更新整数列。您需要计算该值并将其提供给您的函数,即。 "序列&#34 =大于5。看看你的错误,它会告诉你究竟是什么问题。

最有可能更多"正确"这样做的方法是创建一个实际的序列并在查询中使用nextval()函数。当Postgres采用内置的方式做得更好时,无需增加您的支持。

https://www.postgresql.org/docs/9.6/static/sql-createsequence.html https://www.postgresql.org/docs/9.6/static/functions-sequence.html