Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   DBI: number of rows updated? (http://www.velocityreviews.com/forums/t861017-dbi-number-of-rows-updated.html)

Aldric Giacomoni 12-24-2009 01:35 PM

DBI: number of rows updated?
 
Is there a way with the DBI gem to figure out how many rows were
touched? I have the following ugly and naive code which, of course,
doesn't give me the number of rows updated (but works like a charm
otherwise):

def run_query query
begin
dbh = DBI.connect( 'DBI:ODBC:PACS' )
statement_handle = dbh.prepare query
statement_handle.execute
rows = statement_handle.rows
statement_handle.finish
dbh.commit
rescue DBI::DatabaseError => e
puts "An error occurred"
puts "Error code: #{e.err}"
puts "Error message: #{e.errstr}"
dbh.rollback
ensure
# disconnect from server
dbh.disconnect if dbh
end
return rows rescue 0
end

def fix_ultrasounds
rows_updated = 0
[UPDATE_QUERY1, UPDATE_QUERY2].each do |q|
rows_updated += run_query q
end
puts "#{rows_updated} rows updated."
end
--
Posted via http://www.ruby-forum.com/.


yermej 12-24-2009 04:33 PM

Re: DBI: number of rows updated?
 
On Dec 24, 7:35*am, Aldric Giacomoni <ald...@trevoke.net> wrote:
> Is there a way with the DBI gem to figure out how many rows were
> touched?


> * * rows = statement_handle.rows


With Ruby 1.8.4 and DBI 0.1.0 this line works as expected for me. Have
you checked the value of rows right after it's assigned? Maybe there's
something else going on.

Jeremy

Brian Candler 12-25-2009 08:57 AM

Re: DBI: number of rows updated?
 
Aldric Giacomoni wrote:
> statement_handle.execute


Have you checked the return value from this lin?

> return rows rescue 0


The 'rescue 0' is superfluous here, it can never be triggered.

Maybe you want:

return rows || 0

(i.e. if rows is nil, return 0). Note that even if the statement which
assigns to rows has never been executed, it will contain nil.

e.g.

if false
foo = 123
end
puts foo.inspect # shows nil
--
Posted via http://www.ruby-forum.com/.


Aldric Giacomoni 12-28-2009 01:16 PM

Re: DBI: number of rows updated?
 
yermej wrote:
> On Dec 24, 7:35�am, Aldric Giacomoni <ald...@trevoke.net> wrote:
>> Is there a way with the DBI gem to figure out how many rows were
>> touched?

>
>> � � rows = statement_handle.rows

>
> With Ruby 1.8.4 and DBI 0.1.0 this line works as expected for me. Have
> you checked the value of rows right after it's assigned? Maybe there's
> something else going on.
>
> Jeremy


It all looks fine, and works fine when separated from the rest of the
code; so it must be broken somewhere else. Thanks for helping me check
this bit, both of you :)
--
Posted via http://www.ruby-forum.com/.



All times are GMT. The time now is 03:50 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57