错误在哪里?
如果请求硬编码的"test"而不是@edit.text,则成功,但如果使用变量,则请求失败。
require 'sqlite3'
Shoes.app do
db = SQLite3::Database.new("test.db")
db.execute("create table t1 ( one VARCHAR(30), two INTEGER )")
db.execute("insert into t1 ( one, two ) values ('test', 55)")
@edit = edit_line
button 'Search' do
db.execute("select * from t1 where one = ?", @edit.text) do |rows|
@test_out.text = rows
end
end
@test_out = para ''
end
PS 经过一番实验,我以这种方式决定这个问题:
button 'Search' do
text = @edit.text.force_encoding("UTF-8")
db.execute("select * from t1 where one = ?", text) do |rows|
@test_out.text = rows
end
end
但我的文件编码 UTF-8
或如下:
button 'Search' do
text = ''
@edit.text.each_char { |ch| text << ch }
db.execute("select * from t1 where one = ?", text) do |rows|
@test_out.text = rows
end
end
为什么会有这种奇怪的行为?
它有效
db.execute("select * from t1 where one = ?", "#{@edit.text}") do |rows|
@test_out.text = rows
end
但这真的非常非常奇怪。我会试着弄清楚的。