Ruby中的PG :: InvalidDatetimeFormat错误

时间:2016-07-23 08:06:26

标签: ruby-on-rails ruby postgresql psql

脚本:

 System.out.print("Enter name:");
 name=sc.nextLine();

 System.out.print("Enter nric: ");
 sc= new Scanner(System.in);
 nric=sc.nextLine();

 System.out.print("Enter age: ");
 age=sc.nextInt();

 System.out.print("Enter gender(male/female): ");
 sc= new Scanner(System.in);
 gender=sc.nextLine();

 System.out.print("Enter weight:");
 weight=sc.nextInt();

 System.out.print("Enter height: ");
 height=sc.nextDouble();

输出:

require 'pg'

con = PGconn.connect(:dbname => 'employee');
con.exec "SET datestyle TO DMY"

con.prepare 'getid' , "SELECT id FROM users WHERE user = $1 AND date IN ($2)"

str = "'21/07/2016' , '22/07/2016'"
res = con.exec_prepared 'getid' , ['usr1',str]
puts res.values

我的要求是根据用户名和日期获取所有ID。但它给出了上述错误。怎么解决这个问题?

1 个答案:

答案 0 :(得分:1)

将一组实际日期对象传递给PG,驱动程序将为您执行格式化工作:

dates = %w|21/07/2016 22/07/2016|
res = con.exec_prepared 'getid', ['usr1', dates.map(&Date.method(:parse))]