Reddit Clans StuG IV Breakdown

I wrote a quick python script instead of trusty old Java which I am a million times better at. Python people who read my lame script will rage that it looks like Java since that is what I tend to think in. Debug my script and then it won't be bugged:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import psycopg2
import sys


connection = None

try:

    connection = psycopg2.connect(database='panther', user='panther', host='localhost', password='********') 
    cursor = connection.cursor()
    cursor.execute("SELECT id FROM tank WHERE name = 'StuG IV'")          
    tank_id = cursor.fetchone()
    cursor.execute("SELECT DISTINCT(player_id) FROM tank_record WHERE tank_id = %s" % tank_id)
    player_ids = []
    total_players = 0.0
    total_battles = 0.0
    total_wn8 = 0.0
    for player_id in cursor.fetchall():
        player_ids.append(player_id[0])

    for player_id in player_ids:
        cursor.execute("SELECT total_battles FROM player_record WHERE player_id = %d AND date > '2015-01-14' ORDER BY DATE" % player_id)
        battles_start = cursor.fetchone()[0]
        cursor.execute("SELECT total_battles FROM player_record WHERE player_id = %d ORDER BY date DESC" % player_id)
        battles_end = cursor.fetchone()[0]
        cursor.execute("SELECT wn8 FROM player_record WHERE player_id = %d ORDER BY date DESC" % player_id)
        wn8 = cursor.fetchone()[0]
        total_wn8 += wn8
        cursor.execute("SELECT name, clan FROM player WHERE id = %d" % player_id)
        record = cursor.fetchone()
        print record[0], record[1], (battles_end - battles_start)
        if (battles_end - battles_start > 0):
            total_players = total_players + 1
            total_battles = total_battles + battles_end - battles_start

    print "Average battles since 15 Jan:", (total_battles / total_players)
    print "Average WN8 of StuG IV owners:", (total_wn8 / total_players)

except psycopg2.DatabaseError, e:
    print 'Error %s' % e    
    sys.exit(1)


finally:

    if connection:
        connection.close()
/r/WorldofTanks Thread