【问题标题】:Python / Pandas / PyMySQL - %s synthaxPython / Pandas / PyMySQL - %s 合成器
【发布时间】:2018-01-25 05:18:11
【问题描述】:

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

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]

这里的想法是只使用business_id = df['id'][i]所在的行来访问一个巨大的sql并过滤table123

但是,我收到以下错误:

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 合成器时遇到了问题。试图查看他们的手册,但没有找到很好的解释。

有人可以帮忙吗?

【问题讨论】:

    标签: python pandas pymysql


    【解决方案1】:

    你有:

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

    必须是:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2013-06-13
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多