字符串插值问题

时间:2015-01-02 17:30:46

标签: sql ruby

我正在尝试将记录从数据库保存到变量“template_for_editing”

puts "select template id to edit"
template_id_to_edit = gets.chomp
template_for_editing = $db.execute %q{
SELECT * FROM templates WHERE id = #{template_id_to_edit}
}
puts template_for_editing

我收到错误代码:

/usr/lib/ruby/vendor_ruby/sqlite3/database.rb:91:in `initialize': unrecognized token: "#" (SQLite3::SQLException)

1 个答案:

答案 0 :(得分:0)

字符串插值不适用于%q,它适用于"

template_for_editing = $db.execute "SELECT * FROM templates WHERE id = #{template_id_to_edit}"

BTW将未经修改的用户输入传递给SQL查询,就像这是灾难的处方。我是用户,我输入:

1; DROP TABLE templates

我放下你的templates桌子。

相关问题