Use transaction before writing to database
added argument to writeToDB to purge database entries before adding new records
Code design