[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

DBI Question

Dominik Werder

1/7/2005 4:48:00 PM

Hello!

while using dbi (with postgres) I wondered if there is sth similar to
dbhost#select_all(sql) for prepared statements.

I got a hint to do:

s = dbh.prepare(sql)
dbh.select_all s

but this doesnt work (I get a driver error) and I also wondered why there
can't be a #select_all for a prepared statement itself like:

s = dbh.prepare(sql)
array = s.select_all

wouldn't this be much easier?

thanks!
Dominik
5 Answers

Charles Mills

1/7/2005 10:19:00 PM

0


Dominik Werder wrote:
> Hello!
>
> while using dbi (with postgres) I wondered if there is sth similar to

> dbhost#select_all(sql) for prepared statements.
>
> I got a hint to do:
>
> s = dbh.prepare(sql)
> dbh.select_all s
>
> but this doesnt work (I get a driver error) and I also wondered why
there
> can't be a #select_all for a prepared statement itself like:
>
> s = dbh.prepare(sql)
> array = s.select_all
>
> wouldn't this be much easier?

I haven't used the postgres driver but if it is DBI complient you
should be able to do the following:
#######
stmt = db.prepare(sql)
# stmt.bind(*vars) <--- optional
array = stmt.execute.fetch_all
stmt.finish # free resources / locks held by the statement
#######

-Charlie

Charles Mills

1/7/2005 10:28:00 PM

0

I made a mistake in the last post.... see below for correct
(hopefully...) answer :)

Charles Mills wrote:
> Dominik Werder wrote:
> > Hello!
> >
> > while using dbi (with postgres) I wondered if there is sth similar
to
>
> > dbhost#select_all(sql) for prepared statements.
> >
> > I got a hint to do:
> >
> > s = dbh.prepare(sql)
> > dbh.select_all s
> >
> > but this doesnt work (I get a driver error) and I also wondered why
> there
> > can't be a #select_all for a prepared statement itself like:
> >
> > s = dbh.prepare(sql)
> > array = s.select_all
> >
> > wouldn't this be much easier?
>
> I haven't used the postgres driver but if it is DBI complient you
> should be able to do the following:
#######
stmt = db.prepare(sql)
array = stmt.execute(*bind_vars).fetch_all
stmt.finish # free resources / locks held by the statement
#######

-Charlie

Dominik Werder

1/8/2005 12:17:00 PM

0

> stmt = db.prepare(sql)
> array = stmt.execute(*bind_vars).fetch_all
> stmt.finish # free resources / locks held by the statement

thanks a lot!!
there is a small mistake in your code but you pointed me into the right
direction :)
stmt.execute returns nil so you have to do:

stmt.execute
array=stmt.fetch_all

bye!
Dominik

Charles Mills

1/8/2005 3:42:00 PM

0


Dominik Werder wrote:
> > stmt = db.prepare(sql)
> > array = stmt.execute(*bind_vars).fetch_all
> > stmt.finish # free resources / locks held by the statement
>
> thanks a lot!!
> there is a small mistake in your code but you pointed me into the
right
> direction :)
> stmt.execute returns nil so you have to do:
>
> stmt.execute
> array=stmt.fetch_all
>

Oh, looks like the DBI spec doesn't say what #execute should return:
http://ruby-dbi.sourceforge.net/DBI...
IMO returning self would be the most natural...
another option:
array = db.select_all(sql, *bind_vars)

-Charlie

Dominik Werder

1/8/2005 4:52:00 PM

0

> Oh, looks like the DBI spec doesn't say what #execute should return:
> http://ruby-dbi.sourceforge.net/DBI...
> IMO returning self would be the most natural...

100% ack!
perhaps michael reads this too :)

bye!
Dominik