Forum: Ruby ruby sql-query good practice

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Rcmn 7. (Guest)
on 2007-02-01 22:14
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
Simon Kröger (Guest)
on 2007-02-01 22:57
(Received via mailing list)
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
This topic is locked and can not be replied to.