Python function to execute sql query