build SQL query for inserting errors into DB