ruby sql-query good practice

R

Rcmn 73

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
 
S

Simon Kröger

Rcmn said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,742
Latest member
AshliMayer

Latest Threads

Top