如何使用 Ruby DBI 的“select_all”与“执行获取/每次完成”相比

发布于 2024-12-28 06:33:33 字数 890 浏览 0 评论 0原文

这是我使用 DBI 的示例代码:

dbh = DBI.connect("DBI:Mysql:host=#{server};database=mysql", user, pass)
rows = dbh.select_all("SHOW TABLES")

打印的行如下所示:

[["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
 ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
 ["user"]]

这是打印 MySQL 数据库中的最后一个表,但记录总数是正确的。

如果我使用execute-fetch/each-finish序列执行此操作,例如:

sth = dbh.execute("SHOW TABLES")
sth.each do |row|
  rows << row[0]
end
sth.finish

它给了我正确的结果,例如:

["columns_priv", "db", "func", "help_category", "help_keyword", "help_relation",
 "help_topic", "host", "proc", "procs_priv", "tables_priv", "time_zone", "time_z
one_leap_second", "time_zone_name", "time_zone_transition", "time_zone_transitio
n_type", "user"]

Here is my sample code for using DBI:

dbh = DBI.connect("DBI:Mysql:host=#{server};database=mysql", user, pass)
rows = dbh.select_all("SHOW TABLES")

The rows printed look like:

[["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
 ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
 ["user"]]

This is printing the last table in a MySQL database, but the total number of records is proper.

If I do this using execute-fetch/each-finish sequence, something like:

sth = dbh.execute("SHOW TABLES")
sth.each do |row|
  rows << row[0]
end
sth.finish

It gives me proper results like:

["columns_priv", "db", "func", "help_category", "help_keyword", "help_relation",
 "help_topic", "host", "proc", "procs_priv", "tables_priv", "time_zone", "time_z
one_leap_second", "time_zone_name", "time_zone_transition", "time_zone_transitio
n_type", "user"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

┾廆蒐ゝ 2025-01-04 06:33:33

当我使用 DBI.connect("DBI:ODBC:Driver={SQL Server};...") 查询 MS SQL DB 时,也会发生同样的情况。

我的解决方法是强制转换 DBI ::行到数组:

sth = dbh.execute "..."
begin
  return sth.map{ |row| row.to_a }
ensure
  sth.finish
end

我敢打赌 1,000 美元,这个问题与缓冲区重用有关 - 可能的“性能”增强,但产生了令人不快的副作用!

The same is also happenning when I query an MS SQL DB using DBI.connect("DBI:ODBC:Driver={SQL Server};...")

My work around was to forcably convert the DBI::Row to an array:

sth = dbh.execute "..."
begin
  return sth.map{ |row| row.to_a }
ensure
  sth.finish
end

I'll bet $1,000 that the issue is to do with buffer reuse - a possible 'performance' enhancement that has had underirable side effects!

能怎样 2025-01-04 06:33:33

我猜想 dbh.select_all 返回一个 Enumerator 实例,它在每次迭代中生成相同的行。请参阅此伪代码以了解我的意思:

def select_all(query)
  db_row = Row.new
  reader = @connection.execute_reader(query)
  Enumerator.new do |yielder|
    until reader.end?
      db_row.populate_from(reader)
      yielder.yield db_row
      reader.next!
    end
  end
end

因此,如果您使用不带块的 select_all ,则会返回一个枚举器,这基本上会产生相同的 db_row 对象。

这只是一个猜测,但是,我相信真相就在附近。


源代码 fetch 方法定义表明我错了,因为 @row 在每次迭代中都是dup复制的。好吧,错误可能出在堆栈的某个地方。

I guess that dbh.select_all returns an instance of Enumerator, which yields the same row on each iteration. See this pseudocode to understand what I mean:

def select_all(query)
  db_row = Row.new
  reader = @connection.execute_reader(query)
  Enumerator.new do |yielder|
    until reader.end?
      db_row.populate_from(reader)
      yielder.yield db_row
      reader.next!
    end
  end
end

Thus, if you're using select_all without block, an Enumerator will be returned, which basically yields the same db_row object.

This is just a guess, but, I believe the truth is nearby.


The source code fetch method definition says that I was wrong, because @row is duplicated on each iteration. Well, probably the error is somewhere up the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文