python/ mysql/ pandas/ sqlalchemy

I have a df with string type column, and the cell might be empty string ""(no space in between). I want to write the df into mysql like the following:

df.to_sql(name, conn)

and then I want to query the df and filter out the cells with empty string like:

sql = 'select * from <tabname> where col = "" '
df = pd.read_sql_query(sql, conn)

however, it will throw errors:
(pymysql.err.ProgrammingError) (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near \\'by = "" and ds = 20190618\\' at line 1')
I have tried to use like:

sql = "select * from <tabname> where col = '' "
<or>
sql = 'select * from <tabname> where col = \'\'' . 
<or> 
sql = 'select * from <tabname> where col = {0}'.format('')

but neither worked:
(pymysql.err.ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'by = '' and ds = 20190618' at line 1")
the error message showed that it successfully parsed the sql query string but it seemed mysql didn't recognize it.

You can go about it like this:

sql = 'select * from <tabname> where col = \'\' '

As your connection seems to expect single quoted text you can escape the single quotes and use them inside string.

暂无
暂无

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Python: pandas read_sql_query Pandas MemoryError with read_sql_query Pandas read_sql_query with comprehension in params Error with params in Pandas read_sql_query read_sql_query in pandas not working now PySpark equivalent of pandas read_sql_query Python - Pandas read_sql_query automatically rounds after 6 Digits Parametric table name for pandas read_sql_query on Postgres Equivalent of pandas read_sql_query for numpy array? Binding list to params in Pandas read_sql_query with other params
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM