简体   繁体   中英

MySQL query throwing 1064 error

I have a huge data which is stored in mysql db. One of the columns in the database is a long string. One of the strings is "iEdge detected the 'warning' condition 'iedge it" which is stored in string_type. I have to query the database and find how many such strings are there.I am querying from my python program. When I do it using something like

  cur.execute("select count(*) from table1 as tmp where tmp.err_string='"+row[r]+"'")

row[r] contains "iEdge detected the 'warning' condition 'iedge it"

I am getting error 1064 (You have an error in your SQL syntax...). I think it is happening because of some quotes in the string. May I know how to fix this?

Can you try this:

sql = "select count(*) from table1 as tmp where tmp.err_string=%s"
cursor.execute(sql, [row[r]])

Let the MySQL Python library worry about escaping special characters and how to quote your string.

See this SO post for more information.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM