Menu

Problem with backslashes in Text field

Help
2002-04-24
2012-09-19
  • Christoph Frank

    Christoph Frank - 2002-04-24

    I tried the following using MySQLdb:

    Con=MySQLdb.Connect(host="127.0.0.1....)
    Cursor=Con.Cursor()

    Cursor.execute("INSERT into table (path) VALUES ('path\\path'))

    Con.Close

    The result in the column 'path' in MySQL is: 'pathpath'.

    If I enter 'path\\path' with the MySQL shell I get: 'path\path' (that's what I want).

    So it seems that the MySQLdb module 'eats' the backslashes. I traced into the module and saw that everything was fine until the method 'db.query'.

     
    • Andy Dustman

      Andy Dustman - 2002-04-24

      You need to do something like this:

      path = r'path\path'
      Cursor.execute('INSERT into table (path) VALUES (%s)', (path,))

      MySQLdb is not eating the backslashes; Python is.  In Python string, \\ prints as a single \.

      Note the use of a Python "raw string" in the above example, i.e. r'path\path', which causes the backslash to not have any special meaning. Also, by using the parameter passing mechanism to execute, you can be sure strings will be escaped properly.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.