我在使用psycopg2时尝试使用多行f字符串,如下所示:
query = (
f"select tb.id"
f"from someDB.tableA ta"
f"inner join someDB.tableB tb on ta.url = tb.fk_url"
f"where ta.name = '{some_name}'"
f"and tb.type in ('{some_type}')"
f"order by tb.id;"
)
cursor = connection.cursor()
cursor.execute(query)
cursor.fetchall()
我不断收到此错误:
psycopg2.errors.SyntaxError: syntax error at or near "."
LINE 1: select tb.idfrom someDB.tableA tainner join someDB.tableB...
关于如何在psycopg2中使用多行f字符串的任何想法? 如果标准多行也可以使用,我不必使用f字符串。 如果可能的话,我只是喜欢它。
答案 0 :(得分:3)
就像多行字符串一样,只需在其前面放置一个f
:
>>> foo = 'this is foo'
>>> bar = 'this is bar'
>>> longstring = f"""
... foo value = {foo}
... bar value = {bar}
... """
>>> print(longstring)
foo value = this is foo
bar value = this is bar