0
我想要構建SQL查詢傳入spark-redshift閱讀器的「查詢」選項。我試圖使用psycopg2,所以我做這樣的事情:如何在沒有連接的情況下爲postgres(Redshift)生成SQL查詢?
from psycopg2 import sql
query = sql.SQL(
"select * from {} where event_timestamp < {}"
).format(
sql.Identifier("events"),
sql.Literal(datetime.now())
).as_string()
但它告訴我,我需要通過上下文(連接或光標)as_string()
。我無法做到,因爲我沒有任何聯繫。
在這種情況下,我應該使用純字符串格式進行一些轉義嗎?
或者有什麼方法可以傳遞一些模擬上下文嗎?爲什麼它需要連接來建立查詢字符串? SQL查詢是否因連接而改變?