0
我在Ruby中有一個方法來查詢數據庫並打印出一些數據,而我試圖使用一個準備語句來代替。如何將變量傳遞給Ruby中的預準備語句?
這裏是沒有準備好的聲明運作方法:
def print_state_speakers(*states)
puts "STATE SPEAKERS"
state_string = "'#{states.*"', '"}'"
state_speakers = $db.execute("
SELECT name, location
FROM congress_members
WHERE location IN (#{state_string})
ORDER BY location")
state_speakers.each { |rep, location| puts "#{rep} - #{location}" }
end
這是我在同樣的方法嘗試使用事先準備好的聲明:
def print_state_speakers(*states)
puts "STATE SPEAKERS"
state_string = "'#{states.*"', '"}'"
begin
pst = $db.prepare "SELECT name, location
FROM congress_members
WHERE location IN (?)
ORDER BY location"
state_speakers = pst.execute state_string
end
state_speakers.each { |rep, location| puts "#{rep} - #{location}" }
end
這裏就是我所說的方法:
print_state_speakers('NJ', 'NY' , 'ME', 'FL', 'AK')
當我用第一種方法運行文件時,它顯示數據,當我使用第二,它什麼也沒有顯示。它不會拋出錯誤。我覺得語法需要有所不同,以解釋傳入的字符串,但我一直在網上搜索,並搞亂了它一段時間,無法使它工作。任何有關如何解決準備好的聲明的洞察力將不勝感激。
這很有道理。非常感謝! – Teresa