Python / Pandas / PyMySQL - %s 语法

Python / Pandas / PyMySQL - %s synthax

我正在尝试执行以下操作:

import pymsql
import pandas as pd

df:

     id      height_code        name          ...
1    34      123123             Jesus
2    84      3421               Heaps
3    23      45243              Ornitorrincus
...

connection=pymysql.connect(host=xxx,user=xxxx,password=xxxx,db=xxxx)

for i in df.index:
    df2=pd.read_sql("select business_id,name,color from table123 where business_id=%s;",connection) % df['id'][i]

这里的想法只是访问一个巨大的 sql 并仅使用 business_id = df['id'][i]

的行过滤表 123

但是,我收到以下错误:

pandas.io.sql.DatabaseError: Execution failed on sql 'select business_id,nome,qualificacao_id from socio_administrador where business_id=%s;': (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1")

所以,显然我在使用 pymsql 中的 %s synthax 时遇到了问题。试图查看他们的手册,但没有找到很好的解释。

有人能帮忙吗?

你有:

read_sql("... where business_id=%s;",connection) % df['id'][i]

必须是:

read_sql("... where business_id=%s;" % df['id'][i], connection)