[英]Psycopg2 parametrized query with sql format function
我剛剛用 Psycopg2 遇到了這個問題:
cur.execute("select format('%s, %s, %s', 'one', 'two', 'three') from foo where bar = %s", ('baz',))
這會產生元組索引超出范圍異常。
很明顯,Psycopg 占位符和 sql 格式的占位符是有沖突的。 除了在 python 級別格式化字符串之外,還有其他解決方案嗎? 這將非常有幫助,因為我無法直接控制該 sql。 我只是拿它並用一些預先確定的參數執行它......
使用雙百分號:
sql = cur.mogrify("select format('%%s, %%s, %%s', 'one', 'two', 'three') from foo where bar = %s", ('baz',))
print(sql)
出去:
"select format('%s, %s, %s', 'one', 'two', 'three') from foo where bar = 'baz'"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.