adb*_*ads 2 python string psycopg2
我尝试在使用 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()
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误:
psycopg2.errors.SyntaxError: syntax error at or near "."
LINE 1: select tb.idfrom someDB.tableA tainner join someDB.tableB...
Run Code Online (Sandbox Code Playgroud)
关于如何在 psycopg2 中使用多行 f 字符串有什么想法吗?如果标准多线也可以工作,我就不必使用 f 字符串。如果可能的话,我只是更喜欢它。
小智 5
和多行字符串一样,只是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
Run Code Online (Sandbox Code Playgroud)