Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Postgres中引用字符串_Ruby On Rails_Postgresql_Activerecord_Ruby On Rails 3.1_Heroku - Fatal编程技术网

Ruby on rails 如何在Postgres中引用字符串

Ruby on rails 如何在Postgres中引用字符串,ruby-on-rails,postgresql,activerecord,ruby-on-rails-3.1,heroku,Ruby On Rails,Postgresql,Activerecord,Ruby On Rails 3.1,Heroku,在Rails中,我使用MySQL并具有 SnCl.all(:conditions => "col3=\"xx\"") 但是它不适用于我在Heroku的Postgres DB您最好根据您的情况使用where。看看 如评论中所述,大多数数据库对字符串文本使用单引号,对标识符使用双引号。MySQL相当宽松,也会接受字符串文本的双引号,但PostgreSQL(谢天谢地)非常严格。因此,您希望使用单引号: SnCl.all(:conditions => "col3 = 'xx'") 或者

在Rails中,我使用MySQL并具有

SnCl.all(:conditions => "col3=\"xx\"") 

但是它不适用于我在Heroku的Postgres DB

您最好根据您的情况使用
where
。看看


如评论中所述,大多数数据库对字符串文本使用单引号,对标识符使用双引号。MySQL相当宽松,也会接受字符串文本的双引号,但PostgreSQL(谢天谢地)非常严格。因此,您希望使用单引号:

SnCl.all(:conditions => "col3 = 'xx'")
或者使用
,其中

SnCl.where("col3 = 'xx'")
SnCl.where('col3 = ?', 'xx')
SnCl.where('col3 = :col3', :col3 => 'xx')
SnCl.where(:col3 => 'xx')
或者合理使用数据库驱动程序的报价功能:

SnCl.where("col3 = #{SnCol.connection.quote('xx')}")
将最好的保存到最后,这是明智的人使用占位符或散列参数保存到
的方式,其中

SnCl.where("col3 = 'xx'")
SnCl.where('col3 = ?', 'xx')
SnCl.where('col3 = :col3', :col3 => 'xx')
SnCl.where(:col3 => 'xx')

最后一个是Rails最常用的方法,上面两个方法对于更复杂的情况非常有用,其中链接要么太麻烦,要么不起作用(例如当您需要一个or子句时)。

我不知道Rails,但在PostgreSQL中(以及几乎所有其他DBMS中)字符串文本需要用单引号括起来:
“col3='xx'”