xml_in(xml)['grade']
puts YAML.dump(grades)
imported_count = 0
DBI.connect("DBI:ADO:" <<
"Provider=Microsoft.Jet.OLEDB.4.0;" <<
"Data Source=#{database_path}") do |dbh|
grades.each do |grade_raw|
g ={}
grade_raw.each do |key,value|
if value.length == 1
g[key] = value.first
else
g[key] = value
end
end
#g.map! { g.length==1 ? g.first : g}
CHAPTER 12 n CREATING REPORTS WITH RUBY AND MICROSOFT OFFICE 252
sql = "SELECT COUNT(*)
FROM grades
WHERE id=?;"
dbh.select_all(sql, g['id'].to_i) do |row|
count = *row
if count == 0
sql = 'INSERT INTO grades
(id, student,
employer, grade,
class_date, class_name)
VALUES (?,?,?,?,?, ?);'
dbh.do(sql, g['id'], g['student'],
g['employer'], g['grade'],
Date.parse(g['took_class_at']),
g['class']
);
dbh.commit
imported_count = imported_count + 1
end
end
end
end
SWin::Application.messageBox "Done! #{imported_count} records imported.",
"All done!"
rescue
SWin::Application.messageBox $!, "Error while importing"
end
Save this as training_loader.rb.
Before you run the example, make sure that the Rails application you created in the
previous section is running.
Pages:
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361