DBI Question

D

Dominik Werder

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
 
C

Charles Mills

Dominik said:
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
 
C

Charles Mills

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

Charles said:
to

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
 
D

Dominik Werder

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
 
C

Charles Mills

Dominik said:
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_SPEC.html
IMO returning self would be the most natural...
another option:
array = db.select_all(sql, *bind_vars)

-Charlie
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top