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.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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'.
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.