Ruby sql-query good practice


#1

what is the equivalent in ruby of that PHP code ?

<? $sql = "select * from mytable"; $result = mssql_query($sql); while($row = mssql_fetch_array($result)){ $array[] = $row[0]; } ?>

so far i have :

require ‘dbi’
db = DBI.connect(“DBI:ADO:Provider=SQLOLEDB;Data
Source=myserver;Initial Catalog=mydb;User Id=myuser;Password=;”)
sql = db.prepare(“select* from mytable”)
sql.execute

while row=sql.fetch do

p row
end

I’m just interested in a good practice to store in an array from an SQL
query.

sql.finish


#2

Rcmn 73 wrote:

p row
end

I’m just interested in a good practice to store in an array from an SQL
query.

sql.finish

if you just want the first column of all rows:
(you should probably only select that one, but anyway)


require ‘mysql’

class Mysql::Result
include Enumerable
end

db = Mysql::new(“myserver”, “myuser”, “”, “mydb”)

array = db.query(“SELECT * from mytable”).map{|row| row.first}

db.close

btw: anyone knows why the “include Enumerable” isn’t there at
the first place?

cheers

Simon