0

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?

sklearning
  • 223
  • 1
  • 4
  • 12

1 Answers1

0

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.

Community
  • 1
  • 1
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • 1
    Thank you. A small change, in cursor.execute(), the second argument should be an iterable object. So I changed it to cursor.execute(sql,[row[r]]) – sklearning Oct 29 '15 at 07:41