ActiveRecord Where子句 - 单引号错误?

时间:2015-08-21 03:41:33

标签: sql ruby-on-rails ruby ruby-on-rails-4 rails-activerecord

在ActiveRecord中(使用Ruby on Rails),如果@products变量@products = Product.all,我说:

@products.where("name = 'check123' "),它返回一个符合该条件的对象数组,如果我去@products.where('name="check123"')我得到一个错误?

: SELECT "products".* FROM "products" WHERE (name = "check123")
Hirb Error: PG::UndefinedColumn: ERROR:  column "check123" does not exist
LINE 1: SELECT "products".* FROM "products" WHERE (name = "check123"...
                                                      ^

为什么会这样?似乎我必须总是在where子句中使用双引号,并在那里使用任何字符串的单引号?

不应该单引号在这里工作,或者有什么东西我没有得到

其他一些观察结果:

@products.where("cost = '23.0'")有效,虽然23的数据类型为整数而不是字符串? @products.where('cost = 23')有效,所以我知道我可以在where子句

中使用单引号

注意:我知道使用'?' where子句中的语法,以避免sql注入,我故意尝试执行这样的查询。

1 个答案:

答案 0 :(得分:2)

双引号用于命名数据​​库对象(表名,列名,用户名,模式名......),而单引号用于表示要用作值的字符串。

所以您的public class NewClass { public static void main(String[] args) { PairOfDice player1 = new PairOfDice(); PairOfDice player2 = new PairOfDice(); Scanner scan = new Scanner(System.in); int p1 = 33, p2 = 0, turnp1 = 0, turnp2 = 0, signal = 1; while (p1 <= 100 || p2 >= 100) { int newp1total = p1; turnp1 = 0; while (turnp1 <= 20 && signal == 1) { System.out.println("Player 1s Turn!"); int die1 = player1.rollOne(); int die2 = player1.rollTwo(); int sumofdice = player1.sumOfRoll(); System.out.println("Player 1: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice); if (sumofdice == 2) { p1 = 0; turnp1 = 0; signal = -1; System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); } else if (die1 == 1 || die2 == 1) { turnp1 = 0; signal = -1; p1 = newp1total; System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); } else { turnp1 += sumofdice; p1 += sumofdice; System.out.println("Points this turn:" + turnp1); System.out.println("Points this game: " + p1); System.out.println(); signal = 1; } } signal = 1; String yesno = "y"; int newp2total = p2; while (yesno.toLowerCase().equals("y") && signal == 1) { System.out.println("Player 2s Turn!"); int die1 = player2.rollOne(); int die2 = player2.rollTwo(); int sumofdice = player2.sumOfRoll(); System.out.println("Player 2: First Die: " + die1 + " Second Die:" + die2 + " Sum of Roll: " + sumofdice); if (sumofdice == 2) { p2 = 0; turnp2 = 0; signal = -1; System.out.println("Player rolled a two 1s. All players points are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); } else if (die1 == 1 || die2 == 1) { signal = -1; turnp2 = 0; p2 = newp2total; System.out.println("Player rolled a 1. All points on this round are forfeited. Next Players turn."); System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); } else { turnp2 += sumofdice; p2 += sumofdice; System.out.println("Points this turn:" + turnp2); System.out.println("Points this game: " + p2); System.out.println(); System.out.println("Try your luck? Y/N"); yesno = scan.next(); System.out.println(); signal = 1; } } } } } 有意义,因为使用UndefinedColumn: ERROR过滤生成的SQL将为@products.where('name="check123"')。数据库引擎正在尝试查找名为.... where name = "check123"的列,并将其值与列check123的值相匹配。由于您的表中不存在列name,因此您会得到一个&#34;未定义的列名称&#34;错误。

对于您的其他问题:

当您按字符串值过滤整数列时,会发生的情况是,db引擎会将列值隐式转换为字符串以执行搜索。

更新

this question's answers描述了一般的SQL标准。总之,大多数Db引擎遵循大多数ANSI标准,并且通常为数据库对象保留双引号的使用。