python code to make query from postgres table
query = "select species as `flower name` from iris" connection = db_connect(connection_data) cursor = connection.cursor() cursor.execute(""" {} """.format(query))
error:
File "/Users/soubhagyapradhan/Desktop/upwork/report/backend/api/utils/fetch_data.py", line 34, in get_query_result cursor.execute(""" {} """.format(query)) psycopg2.errors.SyntaxError: syntax error at or near "`" LINE 1: select species as `flowers` from iris
I wants to add space to alias name of column thats why i have written my code like this. Plese check how can i fix it.
Advertisement
Answer
Postgres follows the SQL standard and uses double quotes, not backticks, to escape database object names (such as column or table names). Use this version:
query = 'SELECT species AS "flower name" FROM iris' connection = db_connect(connection_data) cursor = connection.cursor() cursor.execute(query)