The structure of the XML
is such that you could have more than one student or class for each row, so it puts them
all in single-element arrays. It??™s easy to work around, though, since you can simply take
the first element of each array to access its single value.
Next, the code connects to the database and loops through each grade from the XML:
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
CHAPTER 12 n CREATING REPORTS WITH RUBY AND MICROSOFT OFFICE 258
g[key] = value
end
end
The loop goes through each grade and pulls out the single value from each member,
which makes it a bit easier to access.
Then you need to check if this particular grade has already been processed:
sql = "SELECT COUNT(*)
FROM grades
WHERE id=?;"
dbh.select_all(sql, g['id'].to_i) do |row|
count = *row
if count == 0
The select_all method calls your block once for each row of the result.
Pages:
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369