# DBI: number of rows updated?

**URL:** <https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635>\
**Category:** Ruby\
**Created:** [December 24, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635 "2009-12-24T14:33:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 24, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635/1 "2009-12-24T14:33:05Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 25, 2009, 9:57am UTC](https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635/2 "2009-12-25T09:57:15Z")

</div>

Aldric G. 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

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 25, 2009, 4:27pm UTC](https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635/3 "2009-12-25T16:27:17Z")

</div>

On Dec 24, 7:35 am, Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![trevoke](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@trevoke](https://www.ruby-forum.com/u/trevoke)\
**Post date:** [December 28, 2009, 2:16pm UTC](https://www.ruby-forum.com/t/dbi-number-of-rows-updated/180635/4 "2009-12-28T14:16:26Z")

</div>

yermej wrote:

> On Dec 24, 7:35ï¿½am, Aldric G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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 🙂
