Move database queries into stats.py