你怎么引述Postgres的字符串 [英] How do you quote strings in Postgres

查看:211
本文介绍了你怎么引述Postgres的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Rails我使用MySQL,并有

In Rails I use MySQL and have

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

,但它不符合我的Postgres数据库中的Heroku工作

but it does not work with my Postgres DB in Heroku

推荐答案

正如在评论中,大多数数据库使用单引号字符串和双引号的标识符。 MySQL是相当宽松,也接受双引号的字符串,但PostgreSQL是(谢天谢地)相当严格。所以,你想使用单引号:

As mentioned in the comments, most databases uses single quotes for string literals and double quotes for identifiers. MySQL is rather lax and will also accept double quotes for string literals but PostgreSQL is (thankfully) quite strict. So you want to use single quotes:

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

或者使用其中,

SnCl.where("col3 = 'xx'")

或合理使用的数据库驱动程序的引用设施:

or with sensible use of the database driver's quoting facilities:

SnCl.where("col3 = #{SnCol.connection.quote('xx')}")

和保存最好的过去,顺便明智的人做到这一点使用占位符或散列参数其中,

And saving the best for last, the way sensible people do it using a placeholder or Hash arguments to where:

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

在最后一节将是最地道的Rails和两个上面,其中链要么过于繁琐或不工作(比如,当你需要或您的WHERE子句中)将是更复杂的病症。

The final one would be the most idiomatic for Rails and the two above it would be useful for more complex conditions where chaining is either too cumbersome or doesn't work (such as when you need an OR in your WHERE clause).

这篇关于你怎么引述Postgres的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆