Ruby DataObjects :: SQLError查询不包含字符串参数

时间:2015-11-07 09:40:59

标签: mysql ruby datamapper

我传递给DataMapper all的字符串参数没有插入到最终查询中并且查询被破坏了:

LeadEmail.all :email => 'gmail'

引发以下错误:

DataObjects::SQLError: You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version 
for the right syntax to use near '' AND `bounced` = FALSE) ORDER BY `id`' at line 1 
(code: 1064, sql state: 42000, query: SELECT `id`, `email`, `bounced` FROM 
`lead_emails` WHERE (`email` = ' AND `bounced` = FALSE) ORDER BY `id`, 
uri: mysql:root@localhost/backup_db?scheme=mysql&user=root&password=mysqlroot&host=localhost&port=&path=/backup_db&query=encoding=utf8mb4&fragment=&encoding=utf8mb4&adapter=mysql)
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `execute_reader'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:147:in `block in read'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:276:in `with_connection'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-do-adapter-1.2.0/lib/dm-do-adapter/adapter.rb:141:in `read'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/repository.rb:162:in `read'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/collection.rb:1118:in `lazy_load'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/support/lazy_array.rb:409:in `each'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/collection.rb:505:in `each'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/collection.rb:977:in `map'
  from /Users/mbpserver/.rvm/gems/ruby-2.1.5/gems/dm-core-1.2.1/lib/dm-core/collection.rb:977:in `inspect'

' gmail'参数永远不会进入最终的SQL查询。有什么想法吗?谢谢

2 个答案:

答案 0 :(得分:0)

对我来说很好:

require 'data_mapper'

# If you want the logs displayed you have to do this before the call to setup
DataMapper::Logger.new($stdout, :debug)

# A Sqlite3 connection to a persistent database
DataMapper.setup(:default, 'sqlite:///Users/7stud/my.db')

class LeadEmail
  include DataMapper::Resource

  property :id,         Serial    # An auto-increment integer key
  property :email,      String    # A varchar type string, for short strings
  property :created_at, DateTime  # A DateTime, for any date you might like.
end

DataMapper.finalize
DataMapper.auto_migrate!

emails = %w[
  gmail
  yahoo
  gmail
].each do |email|
  LeadEmail.create(
    :email => email,
    :created_at => Time.now
  )
end

p LeadEmail.all(:email => 'gmail')

--output:--
$ r 1.rb 
 ~ (0.000105) PRAGMA table_info("lead_emails")
 ~ (0.000013) SELECT sqlite_version(*)
 ~ (0.000054) DROP TABLE IF EXISTS "lead_emails"
 ~ (0.000007) PRAGMA table_info("lead_emails")
 ~ (0.001578) CREATE TABLE "lead_emails" ("id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, "email" VARCHAR(50), "created_at" TIMESTAMP)
 ~ (0.001601) INSERT INTO "lead_emails" ("email", "created_at") VALUES ('gmail', '2015-11-07T03:45:04-03:00')
 ~ (0.001525) INSERT INTO "lead_emails" ("email", "created_at") VALUES ('yahoo', '2015-11-07T03:45:04-03:00')
 ~ (0.001721) INSERT INTO "lead_emails" ("email", "created_at") VALUES ('gmail', '2015-11-07T03:45:04-03:00')
 ~ (0.000036) SELECT "id", "email", "created_at" FROM "lead_emails" WHERE "email" = 'gmail' ORDER BY "id"
[#<LeadEmail @id=1 @email="gmail" @created_at=#<DateTime: 2015-11-07T03:45:04-03:00 ((2457334j,38704s,0n),-25200s,2299161j)>>, #<LeadEmail @id=3 @email="gmail" @created_at=#<DateTime: 2015-11-07T03:45:04-03:00 ((2457334j,38704s,0n),-25200s,2299161j)>>]

答案 1 :(得分:0)

是的,dataobjects gem似乎被mysql 5.7打破了