Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Ruby DBI:如何在使用method时添加execute参数;在(?)中;_Ruby On Rails_Ruby_Dbi - Fatal编程技术网

Ruby on rails Ruby DBI:如何在使用method时添加execute参数;在(?)中;

Ruby on rails Ruby DBI:如何在使用method时添加execute参数;在(?)中;,ruby-on-rails,ruby,dbi,Ruby On Rails,Ruby,Dbi,以下是我使用DBI的示例代码: begin dbh = DBI.connect("DBI:Mysql:Test:localhost", "testuser", "testpassword") sql = "select u.email, u.account_name, u.height, u.weight from test_users where id in (?) group by u.id order b

以下是我使用DBI的示例代码:

begin
  dbh = DBI.connect("DBI:Mysql:Test:localhost", "testuser", "testpassword")

  sql = "select u.email, u.account_name, u.height, u.weight 
         from test_users 
         where id in (?)
         group by u.id
         order by u.id
         into outfile '/tmp/test.csv'
         fields terminated by ','
         enclosed by '\"'
         lines terminated by '\n'"

  sth = dbh.prepare(sql)
  sth.execute('1,2,3')
  sth.finish
rescue DBI::DatabaseError => e
  puts "An error occurred"
  puts "Error code:    #{e.err}"
  puts "Error message: #{e.errstr}"
ensure
  # disconnect from server
  dbh.disconnect if dbh
end
但是它给我的sql,比如“in”方法的值是不正确的:

  select u.email, u.account_name, u.height, u.weight 
  from test_users 
  where id in ('1,2,3')
  group by u.id
  order by u.id
  into outfile '/tmp/test.csv'
  fields terminated by ','
  enclosed by '\"'
  lines terminated by '\n'"
我希望这样(注意更改结果是:“id in(1,2,3)”):


IN子句不是这样工作的,您需要有与参数数量相同的
,在您的情况下,您应该有:

sql = "select u.email, u.account_name, u.height, u.weight 
         from test_users 
         where id in (?,?,?)
         group by u.id
         order by u.id
         into outfile '/tmp/test.csv'
         fields terminated by ','
         enclosed by '\"'
         lines terminated by '\n'"

然后将每个值传递给查询。

更一般地说,您可以使用
map
join
生成占位符:

foo = 'foo'
bar_array = [1, 2, 3]
bar_placeholders = bar_array.map { '?' }.join(', ')
baz = 18
sql = <<-SQL
  SELECT * FROM t
  WHERE foo = ?
  AND bar IN (#{bar_placeholders})
  AND baz > ?
SQL
...
sth.execute(foo, *bar_array, baz)
foo='foo'
bar_数组=[1,2,3]
bar_占位符=bar_array.map{'?'}.join(','))
baz=18

sql=您是否在数组中尝试过它
([1,2,3])
@PraveshKhatri我尝试过,它给我的结果是:“[1,2,3]”我只是在查看sql,而没有查看结果。实际上前面的代码也是正确的。虽然sql看起来不友好,但select结果是正确的。谢谢你的回答,我学到了很多。
foo = 'foo'
bar_array = [1, 2, 3]
bar_placeholders = bar_array.map { '?' }.join(', ')
baz = 18
sql = <<-SQL
  SELECT * FROM t
  WHERE foo = ?
  AND bar IN (#{bar_placeholders})
  AND baz > ?
SQL
...
sth.execute(foo, *bar_array, baz)