The query
returns only one result, so your block is executed only once. The row is an array, so you
use the * operator to pull that single value into the count variable, and check if the result
is zero. If the result is zero, this data is missing from your local database, and you can
insert it as follows:
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
nNote Adding missing records isn??™t the only way to synchronize databases. You could also delete all of the
records every time you import the data from the XML, and simply insert all of the records again. However,
this will cause problems if the end user wants to add new fields to the table. With the current technique,
adding fields isn??™t a problem??”the importer will leave them alone.
CHAPTER 12 n CREATING REPORTS WITH RUBY AND MICROSOFT OFFICE 259
If the import was successful, you pop up a message box telling the user you??™re finished
and how many records were imported.
Pages:
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370