2016-07-23 13 views
0

Script:PG :: InvalidDatetimeFormat Fehler in Ruby

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 

Ausgang:

g.rb:10:in `exec_prepared': ERROR: invalid input syntax for type date: "'21/07/2016' , '22/07/2016'" (PG::InvalidDatetimeFormat) from g.rb:10:in `<main>' 

Meine Forderung ist zu bekommen alle IDs basierend auf Benutzernamen und Datum. Aber es gibt den obigen Fehler. Wie löst man das?

Antwort

1

Pass eine Reihe von realen Datum Objekte zu PG, wird der Fahrer den Job Formatierung für Sie tun:

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