3

我有一个 ruby​​ 脚本,我需要从数据库中检索所有值,并对检索到的每个值进行处理。目前我正在对每次检索进行硬编码,但这仅在有 3 个值返回时才有效。我该如何迭代呢?下面是我的代码:

  require 'pg'
  pg_conn = PGconn.connect(host = "main-pg-db-super.center.com", port = 6433, options = '', tty = '', 
                        dbname = "master_data", login = "user", password = "password")

  all_children = pg_conn.exec("SELECT id FROM pt.tests WHERE parent = '1';") # will return 3 results
  puts all_children[0]['id']
  puts all_children[1]['id']
  puts all_children[2]['id']
4

1 回答 1

9
all_children.each do |child|
  puts child['id']
end

这不起作用吗?

于 2013-04-09T14:09:25.943 回答