minimagician 0 Newbie Poster

Hello All,
Don't know if this is the right place to put this doubt.

Help if possible.
I have a python file that extracts data from a file and adds the required part to the database.
Now MySQL has certain reserved characters, whenever my update query comes across a reserved character within the file, it gives and error and exits the function.

the code for your referral:

import MySQLdb
    import sys

    Con = MySQLdb.Connect(host="127.0.0.1", port=3306, user="local", passwd="xxxx", db="test")
    Cursor = Con.cursor()

    f = open('readfile.txt')
      for line in f:
        if 'From' in line:
          id = line
       
        if 'Found' in line:
          break

      for line in f:
        content = content + line
      print mail
      string2 = "UPDATE Know SET Content = 'this' WHERE Id = '" + id + "'"
      try:
        Cursor.execute(string2)
      except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0], e.args[1])
        sys.exit (1)

      f.close()

Error Statement:
Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'FLSmidth House'

The 'readfile.txt' file has single quotes in it at some places [ ' ], which i know is a reserved character in Mysql. Now such characters can keep on appearing in the file at random positions and the data within readfile.txt is not fixed.

the above code works perfectly if i read from a file which doesn't have any single quote in it. But I fail to understand the changes I need to make so that if a single quote is encountered within the file at any line or any position, it should still update the database and add the contents of the file to the appropriate column.

Thanks a lot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.