Here you seem to be closing the DB before the cursor, which doesn't seem good. Probably put both the cursor and DB close in the finally.