[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ruby sql-query good practice

Rcmn Rcmn

2/1/2007 8:14:00 PM

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

--
Posted via http://www.ruby-....

1 Answer

Simon Kröger

2/1/2007 8:51:00 PM

0

Rcmn 73 wrote:
> 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

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