请求不是由文本决定的

时间:2011-05-23 16:41:15

标签: sql ruby shoes

错误在哪里?

如果请求硬编码'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

为什么会出现这种奇怪的行为?

1 个答案:

答案 0 :(得分:1)

它有效

db.execute("select * from t1 where one = ?", "#{@edit.text}") do |rows|
  @test_out.text = rows
end

但这真的很奇怪。我会试着搞清楚。