DBI: number of rows updated?

Discussion in 'Ruby' started by Aldric Giacomoni, Dec 24, 2009.

  1. 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/.
     
    Aldric Giacomoni, Dec 24, 2009
    #1
    1. Advertisements

  2. Aldric Giacomoni

    yermej Guest

    On Dec 24, 7:35 am, Aldric Giacomoni <> 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
     
    yermej, Dec 24, 2009
    #2
    1. Advertisements

  3. 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/.
     
    Brian Candler, Dec 25, 2009
    #3
  4. yermej wrote:
    > On Dec 24, 7:35�am, Aldric Giacomoni <> 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/.
     
    Aldric Giacomoni, Dec 28, 2009
    #4
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ulloa
    Replies:
    1
    Views:
    676
    Juha Laiho
    Jul 22, 2004
  2. Subba Rao via DotNetMonster.com

    script for moving rows up and down and traverse thru rows of HTML table

    Subba Rao via DotNetMonster.com, Mar 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    8,865
    Subba Rao via DotNetMonster.com
    Mar 19, 2005
  3. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    607
    Dmitry Korolyov
    Sep 22, 2003
  4. JDP@Work

    DataSet Rows Updated to usp_MyUpdateProc and more q's....

    JDP@Work, Oct 2, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    239
    JDP@Work
    Oct 2, 2004
  5. Jerome Hauss
    Replies:
    0
    Views:
    315
    Jerome Hauss
    Oct 13, 2004
  6. Asby

    Mason, DBI, and DBI::Pg

    Asby, Jul 24, 2003, in forum: Perl Misc
    Replies:
    0
    Views:
    327
  7. Tim Haynes
    Replies:
    3
    Views:
    308
    Ron Reidy
    Sep 13, 2003
  8. doug
    Replies:
    1
    Views:
    216
    Julia De Silva
    Jun 7, 2005
Loading...