将参数传递给 PostgreSQL 查询时出现 Python TypeError : not all arguments converted during string formatting,

标签 python string postgresql

我知道以前有人问过这个问题,但任何答案对我都有帮助,我在 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/

相关文章:

Django 1.2 PostgreSQL 使用 ON DELETE NO ACTION 级联删除键

python - 如何更改 folium/geojson 颜色

python - python 在计算机中如何存储字符串?

php计算字符串值: "2+4+3-12+3-5"

postgresql - Postgres : Row Level Security doesn't work with views

postgresql - 当 pg_cancel_backend 不起作用时,我应该如何终止进程?

python - 从文件中读取文件

python - Django 抽象模型与简单的 Python 混合与 Python ABCs

python - 如何获得 python 模块的点符号?

C#高级查询语句字符串处理