我知道以前有人问过这个问题,但任何答案对我都有帮助,我在 python 中使用 Basic 模块 (psycopg2) 试图将外部参数 row[1] 传递到Postgres 查询是:
print row[1], type(row[1])
630776863141535744
<type 'str'>
当我运行以下代码时,我遇到了TypeError: not all arguments converted during string formatting
for row in rows:
t=row[1]
PSQL1="SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);"
c.execute(PSQL1,t)
我也试过下面的代码,也有同样的错误:
for row in rows:
c.execute("SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);",row[1])
我已经改变了许多其他部分,比如把 '%s'
而不是 %s
但我仍然有错误!
最佳答案
execute()
的第二个参数在您的情况下应该是一个元组:
for row in rows:
c.execute("SELECT occorrenza.tag from occorrenza where tweet=%s and tag in ( select pk_tag from hashtag where isentity=FALSE);", (row[1], ))
关于将参数传递给 PostgreSQL 查询时出现 Python TypeError : not all arguments converted during string formatting,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34842450/