python代碼從postgres表中查詢
query = "select species as `flower name` from iris"
connection = db_connect(connection_data)
cursor = connection.cursor()
cursor.execute(""" {} """.format(query))
錯誤:
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
我想為列的別名添加空格,這就是我這樣撰寫代碼的原因。請檢查我該如何解決。
uj5u.com熱心網友回復:
Postgres 使用雙引號而不是反引號來轉義資料庫物件名稱(例如列名或表名)。使用這個版本:
query = 'SELECT species AS "flower name" FROM iris'
connection = db_connect(connection_data)
cursor = connection.cursor()
cursor.execute(query)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/432386.html
標籤:Python sql PostgreSQL
上一篇:Java崗大廠面試百日沖刺【Day54】— Redis4 (榷訓月累,每日三題)
下一篇:基于兩列連接兩個資料框
