Ruby 黄瓜中的 Postgresql 记录集输出显示
Postgresql recordset output display in Ruby cucumber
我在 ruby 中显示 postgresql 输出时遇到困难,而且如果有任何方法可以在 html 中打印相同的输出。
下面是代码和错误:
Then(/^selecting source table$/) do
require 'pg'
conn = PGconn.connect("localhost",5433,'','', "db", "postgres", "pass")
result = conn.exec('select EMP_ID,DEPT_ID,DEPT_NAME,EMP_NAME from EMP_TABLE')
result.each do |row|
puts row['EMP_ID'] + ' | ' + row['DEPT_ID'] + ' | ' + row['DEPT_NAME'] + ' | ' + row['EMP_NAME']
end
end
错误:
NoMethodError: undefined method +' for nil:NilClass
./features/step_definitions/my_steps.rb:45:inblock (2 levels) in ' ./features/step_definitions/my_steps.rb:44:in each'
./features/step_definitions/my_steps.rb:44:in/^selecting source table$/' ./features/PG.feature:12:in `Then selecting source table' 1 scenario (1 failed) 1 step (1 failed) 0m0.072s
您的一个 row[whatever]
值是零。如果将所有内容都转换为字符串,它应该可以工作。
result.each do |row|
puts row['EMP_ID'].to_s + ' | ' + row['DEPT_ID'].to_s + ' | ' + row['DEPT_NAME'].to_s + ' | ' + row['EMP_NAME'].to_s
end
更好的是,您可以使用 .join()
方法来做到这一点,同时清理您的逻辑。
result.each do |row|
puts row.join(' | ')
end
编辑!
不好意思,那是散列,不是数组。给我10分钟,我给你正确答案...
编辑 2
因为是散列,需要在里面加上.values
方法
result.each do |row|
puts row.values.join(' | ')
end
我在 ruby 中显示 postgresql 输出时遇到困难,而且如果有任何方法可以在 html 中打印相同的输出。
下面是代码和错误:
Then(/^selecting source table$/) do
require 'pg'
conn = PGconn.connect("localhost",5433,'','', "db", "postgres", "pass")
result = conn.exec('select EMP_ID,DEPT_ID,DEPT_NAME,EMP_NAME from EMP_TABLE')
result.each do |row|
puts row['EMP_ID'] + ' | ' + row['DEPT_ID'] + ' | ' + row['DEPT_NAME'] + ' | ' + row['EMP_NAME']
end
end
错误:
NoMethodError: undefined method +' for nil:NilClass
./features/step_definitions/my_steps.rb:45:inblock (2 levels) in ' ./features/step_definitions/my_steps.rb:44:in each'
./features/step_definitions/my_steps.rb:44:in/^selecting source table$/' ./features/PG.feature:12:in `Then selecting source table' 1 scenario (1 failed) 1 step (1 failed) 0m0.072s
您的一个 row[whatever]
值是零。如果将所有内容都转换为字符串,它应该可以工作。
result.each do |row|
puts row['EMP_ID'].to_s + ' | ' + row['DEPT_ID'].to_s + ' | ' + row['DEPT_NAME'].to_s + ' | ' + row['EMP_NAME'].to_s
end
更好的是,您可以使用 .join()
方法来做到这一点,同时清理您的逻辑。
result.each do |row|
puts row.join(' | ')
end
编辑!
不好意思,那是散列,不是数组。给我10分钟,我给你正确答案...
编辑 2
因为是散列,需要在里面加上.values
方法
result.each do |row|
puts row.values.join(' | ')
end