Hi,
This isn''t related to Rails itself, but rather to Ruby. I''m
trying to
import a file into a database. The fields are separated by a
''|''.
Everything seems to work fine, but after 64 rows are inserted, it starts
mangling the field values. Would you take a look at the following code?
require ''mysql''
def capitalize(str)
str.downcase.gsub(/\b\w/) {|first| first.upcase }
end
def state(name, h)
id = -1
h.query("SELECT id FROM states WHERE name =
''#{name}''").each do |row|
id = row[0]
end
if id != -1
return id
end
short = name.gsub(/ /, ''-'').downcase
h.query("INSERT INTO states (name, url_name)
VALUES(''#{name}'',
''#{short}'')")
return h.insert_id()
end
dbh = Mysql.real_connect(...)
rows = 0
File.open($*[0]) do |file|
file.each do |row|
rows = rows.next
row = capitalize(row)
row = row.gsub(/ Y /, '' y '')
row = row.gsub(/ Para /, '' para '')
row = row.gsub(/ En /, '' en '')
row = row.gsub(/ Con /, '' con '')
row = row.gsub(/ De /, '' de '')
row = row.gsub(/ Del /, '' del '')
row = row.gsub(/Sa de Cv/, ''S.A. de C.V.'')
row = row.gsub(/S.A de/, ''S.A. de'')
row = row.gsub(/de C.V./, ''de CV'')
row = row.gsub(/de CV/, ''de C.V.'')
row = row.gsub(/Mexico/, ''México'')
row = row.gsub(/mexico/, ''México'')
cname, st, county, street, street2, zip, phone, fax, email, cat,
dummy, dummy2, dummy3 = row.chomp.split(/\s*\|\s*/)
puts state(st, dbh)
#todo: insert record...
end
end
puts rows
puts "inserted"
This is a sample row (which is the one that actually gets mixed up):
ARALY ALIMENTOS S.A DE C.V|JALISCO|EL SALTO|CALLE CARRET EL SALTO 2054-A
|PUEBLO EL SALTO|45680 | (33)36893084 | | |EMBUTIDO DE CARNES
FRÍAS|621006|30|De 0 a 50
st should be ''JALISCO'' right? Well, it gets assigned to
"EL SALTO"
instead... if I run irb and do this:
row = "ARALY ALIMENTOS S.A DE C.V|JALISCO|EL SALTO|CALLE CARRET EL SALTO
2054-A |PUEBLO EL SALTO|45680 | (33)36893084 | | |EMBUTIDO DE CARNES
FRÍAS|621006|30|De 0 a 50"
cname, st, county, street, street2, zip, phone, fax, email, cat, dummy,
dummy2, dummy3 = row.chomp.split(/\s*\|\s*/)
st gets correctly assigned to "JALISCO"... so I''m really lost
on why
this behavior occurs on my code...
Could you please help me out?
Thanks!