我有一个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']
答案 0 :(得分:9)
all_children.each do |child|
puts child['id']
end
这不起作用吗?