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.