throw sql errors on sql commands and refactor code to make it more reusable