Fix Python3: NameError: global name 'unicode' is not defined

This PR does 2 things.

1. better handle str type checking to prevent errors in python3
2. removes a .encode call that was causing postgresql syntax errors.