Ruby数据库连接多次返回相同的记录

时间:2020-02-04 20:32:50

标签: ruby oracle dbi

我继承了一个Ruby脚本,该脚本已成功连接到数据库并计算了行数:

# https://github.com/r-dbi/bigrquery
library(bigrquery)

# replace this with your project ID 
billing <- "your_ID" 

sql <- "SELECT * FROM `your_table`"
tb <- bq_project_query(billing, sql)

#> Auto-refreshing stale OAuth token.
df <- bq_table_download(tb, max_results = 10)

df1 <- data.frame(df)
componentid = 5
components=conn.select_all("select * from component_details where componentid=?", componentid)
puts components.count.to_s + " total components   #outputs correct number of components

返回

components.each do |component|
  puts component["ORGID"].to_s      #outputs same ORGID from first record in the database
end

但是,在我的Ruby脚本中运行它时,它仅返回第一行两次。直接从Oracle数据库运行时:

123
123

我不确定为什么不循环遍历每行并显示每条记录的正确ID,而只显示两次第一条记录的ID(在上面的示例中)。我目前无法更新版本和/或数据库gem。

select * from component_details where componentid='5';    # returns 2 distinct rows

ID  | ORGID  | COMPONENTID
1   | 123    |  5
2   | 456    |  5

1 个答案:

答案 0 :(得分:1)

我在“ Ruby DBI select_all vs execute-fetch/each-finish ”中找到了答案。

select_all显然重用了缓冲区,并且产生了无法预料的后果,因此我改用execute