Fixed database code so it inserts unique rows only.