Resubmit of patch #1660035. There was a conflict with one of the other patches.
General cleanups to db.py.
- Moved all imports to top.
- Removed unused imports.
- Removed deprecated string module references. Change to use new string
- Corrected catchall except: to be more specific where I could (only one
place for now).