Prevent inconsistencies in download statistics

Download script also uses DBConnection now,
SQL changed to reflect new primary keys and relations