Ruby DBI fetch can't handle all-zero dates in MySQL -
i'm trying access our database simple cgi/ruby , dbi:
#!/usr/bin/ruby -w require "dbi" dbh = dbi.connect("dbi:mysql:my:mydb", "xxxx", "xxxx") ... query = "select eta mydb.mytable id = #{someval}" rows = dbh.execute(query) while row = rows.fetch() # stuff ... end
that works fine of time, hit record broke error:
/usr/lib/ruby/vendor_ruby/dbd/mysql.rb:120:in `parse': invalid date (argumenterror) /usr/lib/ruby/vendor_ruby/dbd/mysql.rb:120:in `parse' /usr/lib/ruby/vendor_ruby/dbi/row.rb:66:in `block in convert_types' /usr/lib/ruby/vendor_ruby/dbi/row.rb:65:in `each' /usr/lib/ruby/vendor_ruby/dbi/row.rb:65:in `each_with_index' /usr/lib/ruby/vendor_ruby/dbi/row.rb:65:in `convert_types' /usr/lib/ruby/vendor_ruby/dbi/row.rb:75:in `set_values' /usr/lib/ruby/vendor_ruby/dbi/handles/statement.rb:226:in `fetch' /usr/lib/cgi-bin/test:39:in `block in <main>' /usr/lib/cgi-bin/test:36:in `each' /usr/lib/cgi-bin/test:36:in `<main>'
after bit of detective work found had date of 0000-00-00 fetch()
doesn't like. undefined dates ok, , dbi in perl can handle 0 dates, it's dbi in ruby.
i can fix database, , i'll try app wrote value database fixed too, think ruby should resilient such things. there way work around this, maybe using rescue
somehow?
this solution came with:
query = "select eta mydb.mytable id = #{someval}" rows = dbh.execute(query) begin while row = rows.fetch() # stuff ... end rescue exception => e puts "#{e}<br>\n" retry end
this works quite nicely though retry starting new while loop, rows maintains state fetch resumes on next record.
the problem it's hard identify bad record(s). fix issued more queries without offending field. ugly solution:
results = hash.new hit_error = false query = "select eta,unique_id mydb.mytable id = #{someval}" rows = dbh.execute(query) begin while row = rows.fetch() # stuff ... results[row[1]] = row[0] end rescue exception => e hit_error = true retry end if hit_error query = "select unique_id mydb.mytable id = #{someval}" rows = dbh.execute(query) while row = rows.fetch() id = row[0] unless results.has_key?(id) begin query = "select eta mydb.mytable unique_id = #{id} limit 1" error = dbh.execute(query) error.fetch() # expect hit same error before puts "unexpected success unique_id #{id}<br>\n" rescue exception => e puts "#{e} @ unique_id #{id}<br>\n" end end end end
finally i'm not sure how valid use dbi/ruby, seems deprecated. same mysql/ruby. tried sequel little success.
Comments
Post a Comment