【发布时间】:2017-10-11 14:45:29
【问题描述】:
我想构建 SQL 查询以传递给spark-redshift 读者的“查询”选项。我正在尝试使用 psycopg2,所以我做了这样的事情:
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 查询是否会因连接而异?
【问题讨论】:
标签: python sql postgresql amazon-redshift psycopg2