【问题标题】:PGError: ERROR: syntax error at or near E'String'PGError:错误:E'String'处或附近的语法错误
【发布时间】:2010-12-01 17:57:30
【问题描述】:

我的 Ruby on Rails 条件适用于 MySQL 但不适用于 PostGres

:conditions => ["A between ? and ? and B between ? and ? and C between ? and ? and ddd is ?",@amin,@amax,@bmin,@bmax,@cmin,@cmax,params[:input].to_s]

产生错误:

ActiveRecord::StatementInvalid (PGError: ERROR:  syntax error at or near "E'word'"
LINE 1: ...c between -100 and 100 and ddd is E'word')

我知道没有最后一次比较它就可以工作。例如。

    :conditions => ["A between ? and ? and B between ? and ? and C between ? and ?",@amin,@amax,@bmin,@bmax,@cmin,@cmax]

ddd 是 :string 类型,而 a、b 和 c 都是 :decimal。

在对 Postgresql 进行一些研究之后,似乎 E' 字符串是转义字符串 - 这对我没有帮助,因为我似乎无法纠正它。我不经常使用 Postgresql,所以如果有明显问题,我提前道歉。

我正在使用 heroku 进行托管,因此我无法更改任何 Postgresql 设置。无论如何,我更愿意真正了解问题所在以及如何解决它。

干杯, 斯图

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    尝试改变

    ddd is ?
    

    进入

    ddd = ?
    

    【讨论】:

      【解决方案2】:

      我相信IS 运算符不能用于比较文本值。你的意图是什么?

      【讨论】:

      • 我使用 'is' 因为它保持可读性并在 MySQL 中工作,但我可以很容易地使用 = 运算符。我会试试的 - 为小费欢呼。
      • 在你的情况下它在 MySQL 中不起作用,它是一个布尔运算符而不是字符串运算符:dev.mysql.com/doc/refman/5.5/en/… 这是你代码中的一个错误。
      【解决方案3】:

      你试过 ddd = 吗?而不是?

       :conditions => ["A between ? and ? and B between ? and ? and C between ? and ? and ddd = ?",@amin,@amax,@bmin,@bmax,@cmin,@cmax,params[:input].to_s]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-20
        • 1970-01-01
        • 2017-07-15
        • 2010-12-24
        • 2016-07-10
        • 2018-11-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多