问题描述
这是我使用DBI的示例代码:
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")
打印的行如下:
[["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"], ["user"],
["user"]]
这正在打印MySQL数据库中的最后一个表,但是记录总数是正确的。
This is printing the last table in a MySQL database, but the total number of records is proper.
如果我使用execute-fetch / each-finish序列执行此操作,则类似于:
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"]
推荐答案
相同当我使用 DBI.connect( DBI:ODBC:Driver = {SQL Server}; ...)查询MS SQL DB时也会发生这种情况
我的解决方法是将DBI :: Row强制转换为数组:
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
我打赌$ 1,000,问题出在缓冲区重用上-一种可能的性能增强,具有不良的副作用!
I'll bet $1,000 that the issue is to do with buffer reuse - a possible 'performance' enhancement that has had underirable side effects!
这篇关于如何使用Ruby DBI的'select_all'与'execute-fetch / each-finish'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!