开发者

SQL Search in Ruby

Ok, this seems like a duplicate question to this: SQL Super Search but it's a different approach. Before I was looking for a lean and efficient way to do this entirely on the database side, but now I was wondering开发者_运维知识库 if anyone knoew how to do something like this in Ruby.

I've tried this, and while I can run a basic

*SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS*  

I can't seem to run subsequent queries without getting

' WARNING: #<ODBC::Statement:0x2c73e84> was not dropped before garbage collection. '

Can someone show me a really easy way to just run sql text and parse the results (and ideally run more queries based on those results)?

EDIT: To clarify, my db code so far is along the lines of:

oConn = DBI.connect('DBI:ODBC:AX')
oConn2 = DBI.connect('DBI:ODBC:AX')

sth = oConn.execute("Select table\_name, column\_name from information\_schema.columns")
sth.fetch do |row|
  table = row["table\_name"]
  column = row["column\_name"]
  puts table + "," + column
 #Dynamic sql here
  sth2.fetch do |row2|
    puts row2[0]
  end
end
sth.finish


That warning is a feature, according to Christian Werner. Try oConn.finish after oConn.execute and don't forget to .disconnect in the end.

irb(main):033:0> require 'dbi'
=> false

irb(main):034:0> oConn=DBI.connect('DBI:ODBC:Blacklisted')
=> #<DBI::DatabaseHandle:0x2d50af0 @trace_mode=2, @handle=#<DBI::DBD::ODBC::Database:0x2d50a3c @attr={}, @handle=#<ODBC::Database:0x2d50a64>>, @trace_output=#<IO:0x2846adc>>

irb(main):035:0> sth = oConn.execute("Select * from blacklistednews where id=12140")
=> #<DBI::StatementHandle:0x2d4c838 @trace_mode=2, @fetchable=true, @row=[nil, nil, nil, nil, nil, nil], @handle=#<DBI::DBD::ODBC::Statement:0x2d4c784 @arr=[],@params=[], handle=#<ODBC::Statement:0x2d4c7c0>>, @cols=["id", "title", "url","description", "pubdate", "synced"], @trace_output=#<IO:0x2846adc>, @prepared=false>

irb(main):036:0> sth.finish
=> nil

irb(main):037:0> oConn.disconnect
=> nil

irb(main):038:0>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜