我有一個元組,如下圖所示
我有一個元組,如下圖所示
vilist = (1, 2,3, 4)
我試圖在Psycopg2查詢中使用它們,如下圖所示
sql = "select * from temp.table1 where ids in {}"
cur.execute(sql, vilist)
然而,我得到一個錯誤,如下圖所示
SyntaxError Traceback (most recent call last)
<ipython-input-91-64f840fa2abe> in < module>
1 sql = "select * from temp.table1 where ids in {}"/span>
----> 2 cur.execute(sql,vilist)
SyntaxError: 語法錯誤在or附近"{"。
行 1: ...rom temp.table1 where ids in {}。
請幫助我解決這個錯誤吧。
uj5u.com熱心網友回復:
使用SQL字串組成來傳遞識別符號或字面符號到查詢文本。
import psycopg2
import psycopg2.sql as sql
# ...
vilist = (1,2, 3, 4)
query = sql.SQL("select * from temp.table1 where ids in {}").format(sql.Literal(vilist)
cur.execute(query)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/324734.html
標籤:
