/ Published in: Ruby
Sometimes I use irb as my database console, DBI is really handy for talking to different db's. Pass this method the result of conn.execute to get it nicely formatted on your screen. (it's a hack, should be more DRY)
Expand |
Embed | Plain Text
def form(re) re.each do |r| i,cols,vals = 0,'|','|' r.column_names.each do |n| cols << "%-20s|" % n vals << "%-20s|" % r[n].to_s[0..19] if ((i+=1) == 5) puts cols puts "+" + (("%s+" % (" "*20))*5) puts vals puts "+" + (("%s+" % ("="*20))*5) i,cols,vals = 0,'|','|' end end puts cols puts "+" + (("%s+" % (" "*20))*5) puts vals puts "+" + (("%s+" % ("="*20))*5) puts "+"*106 end end conn=DBI.connect('DBI:....', 'uname', 'pwd') form(conn.execute('select * from table')
You need to login to post a comment.
