[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

read tables from SQL code

Bu Mihai

10/20/2007 7:01:00 AM

hi,
i have a SQL code like this one:

SELECT Select_List
FROM Table_List
[WITH (BUFFERING = lExpr)]
[WHERE Conditions]
[GROUP BY Column_List]
[UNION Clause]
[HAVING Conditions]
[ORDER BY Column_List]
[INTO Clause | TO Clause ]
[Additional_Display_Options]

can i read/save with ruby the columns involved in that SQL select?
--
Posted via http://www.ruby-....

2 Answers

Sharon Rosner

10/20/2007 7:56:00 AM

0

> can i read/save with ruby the columns involved in that SQL select?

If you use Sequel you can retrieve the columns for arbitrary SQL like
this:

require 'sequel/mysql' # assuming you're using MySQL
DB = Sequel('mysql://localhost/mydb')
DB['select * from items'].columns #=> [:id, :name, ...]

But if you're already using Sequel why not construct your queries
using Ruby instead of SQL, e.g.:

dataset = DB.query do
from :items
where {:name =~ /^abc/ && :price < 100}
order_by :name
end
p dataset.columns
dataset.each {|r| p r}

You can find more information about Sequel here:

http://code.google.com/p/r...

And you can also get help on Sequel-talk:

http://groups.google.com/group/s...

best
Sharon


John Joyce

10/20/2007 5:12:00 PM

0

Ruby DBI will let you do exact SQL, like DBI in other languages.

ActiveRecord can do some of that select statement, but some of it no.
ActiveRecord does have the ability to execute direct SQL statements
though.