pandas read_sql with parameters and wildcard operator

前端 未结 2 1255
死守一世寂寞
死守一世寂寞 2021-01-16 10:45

I am trying to retrieve data from sql through python with the code:

query = (\"SELECT stuff FROM TABLE WHERE name like %%(this_name)s%\")
result = pd.read_sq         


        
相关标签:
2条回答
  • 2021-01-16 11:13

    Try using a docstring and some quotes around the like pattern. It appears you are using the pyformat paramstyle.

    query = """SELECT stuff FROM TABLE WHERE name LIKE '%%(this_name)s%'"""
    results = pd.read_sql(query, con=cnx, params={'this_name': some_name})
    
    0 讨论(0)
  • 2021-01-16 11:33

    Consider concatenating the wildcard operator, %, to passed in value:

    query = ("SELECT stuff FROM TABLE WHERE name LIKE %(this_name)s") 
    result = pd.read_sql(query,con=cnx, params={'this_name': '%'+ some_name +'%'})
    
    0 讨论(0)
提交回复
热议问题