Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

postgres column allow space to column while making alias name

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Postgres 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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading