SQL Search in Ruby

Ok, this looks like a duplicate question: SQL Super Search but this is 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 could suggest how to do something like this in Ruby.

I tried this and although I can run a basic

*SELECT table_name, column_name FROM INFORMATION_SCHEMA.COLUMNS*  

      

I cannot run subsequent requests without receiving

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

      

Can anyone 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 still goes along the lines:

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

      

+2


source to share


1 answer


This warning is according to Christian Werner . Try oConn.finish

after oConn.execute

and don't forget at the end .disconnect

.



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>

      

+2


source







All Articles