python - How to Use a Wildcard (%) in Pandas read_sql() -
i trying run mysql query has text wildcard in demonstrated below:
import sqlalchemy import pandas pd #connect mysql database engine = sqlalchemy.create_engine('mysql://user:@localhost/db?charset=utf8') conn = engine.connect() #read sql pandas dataframe mysql_statement = """select * table field '%part%'; """ df = pd.read_sql(mysql_statement, con=conn)
when run error shown below related formatting.
typeerror: not enough arguments format string
how can use wild card when reading mysql pandas?
under hood of pandas, it's using whatever sql engine you've given parse statement. in case that's sqlalchemy, need figure out how handles %
. might easy escaping like '%%part%%'
.
in case of psycopg, use params
variable this:
mysql_statement = """select * table field %s; """ df = pd.read_sql(mysql_statement, con=conn, param=("%part%",))
Comments
Post a Comment