我正在使用 parseexcel gem 读取 excel 文件.. 只要我将内容打印到控制台,一切都可以正常工作。 我创建了一个 rake 任务来将数据插入数据库.. 所以我有一堆创建语句。像这样
description = row[i+1].to_s
Coa.create(:is_group => true, :ag_gl_code => "AGL_001", :description => "#{description}")
我的问题是“描述”变量。它是一个字符串字段。如果我像这样将描述打印到控制台,它会工作并打印值
puts "{#description}" #prints the value 'Estate'
但是在创建时它给我一个sql错误
SQLite3::SQLException: unrecognized token: "'E":
但查询生成正常。该字符串在查询中带有单引号,如果用硬编码字符串替换变量,它就可以正常工作。我尝试了很多组合,比如使用 to_s 等。但无法让它工作。
如有任何帮助,我们将不胜感激。谢谢
这是我的架构
create_table "coas", :force => true do |t|
t.string "ag_gl_code"
t.string "description"
t.boolean "is_group"
t.datetime "created_at"
t.datetime "updated_at"
end
这里是终端日志,我已经记录了'description'的值,注意它打印的是值,没有额外的引号。
Estate
rake aborted!
SQLite3::SQLException: unrecognized token: "'E": INSERT INTO "coas" ("created_at", "is_group" , "description", "ag_gl_code", "updated_at") VALUES ('2011-07-14 14:48:56.616150', 't', 'Estate', 'AGL_001', '2011-07-14 14:48:56.616150')
最佳答案
好的,我修好了。最后。
description.to_s("latin1")
是sqlite编码的问题。
关于ruby-on-rails - Ruby/Rails 转换为字符串错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6690107/