use contextlib.closing() instead of explicitly closing conn & cur
split raising cases