tags:

views:

261

answers:

5

Hi all

Is there a Python equivalent of PHP's mysql_real_escape_string?

I'm trying to insert some strings into a MySQL db direct from Python, and keep getting tripped up by quotes in the strings.

mysql_string = "INSERT INTO candidate (name, address) VALUES  " 
for k, v in v_dict.iteritems():
    mysql_string += " ('" + v_dict['name'] + "', '" + v_dict['address'] + "'), "
mysql_string += ";"
cursor.execute(mysql_string)

I've tried re.escape() but that escapes every non-alphanumeric character in the strings, which isn't what I need - I just need to escape single quotes in this instance (plus more generally anything else that might trip up MySQL).

Could do this manually I guess, but is there a smarter way to do it in Python?

+2  A: 

In this particular case you just need to use executemany method of the cursor.

mysql_string = "INSERT INTO candidate (name, address) VALUES  (%s,%s);"
cursor.executemany(mysql_string, v_dict.iteritems())
newtover
+2  A: 

If you are using mysql-python, just try

MySQLdb.escape_string(SQL)

Example

>>> import MySQLdb
>>> MySQLdb.escape_string("'")
"\\'"
S.Mark
Thought it would be something simple. Thanks!
AP257
-1 This is the wrong answers in several ways. The OP would be better off using cursor.executemany() or using a bulk load tool if he is starting from a csv file.
hughdbrown
You may check [MySQLdb's documentation](http://mysql-python.sourceforge.net/MySQLdb.html#id5). its exactly an equivalent of `mysql_real_escape_string`. I know, *My* answer did not cover all, but its not wrong. But ok, you could **disagree**.
S.Mark
+2  A: 

MySQLdb.escape_string is equivalent. All of that is described in documentation.

Łukasz
+6  A: 
cursor.executemany('INSERT INTO candidate (name, address) VALUES (%s, %s)',
                   [(v_dict['name'], v_dict['address'])] * len(v_dict))

should do what your code appears to attempt -- inserting the same identical values N times (you're looping on v_dict.iteritems() but completely ignoring the loop variables, and instad just reusing those specific two values from v_dict each and every time). Of course if you mean something completely different the second line will need to be changed accordingly, but the key idea anyway is to use placeholders, not string formatting, for such tasks.

Alex Martelli
Alex is right: using executemany() this way avoids the formatting issues and all the possibilities for incorrect SQL. And @AP257 you are doing something crazy with your dictionary, as Alex intimates.
hughdbrown
+1  A: 

Is there a Python equivalent of PHP's mysql_real_escape_string?

Yes, it's escape_string on the database connection object.

Not escape_string on the MySQLdb module, which is equivalent to mysql_escape_string in PHP, and has the same potential vulnerability with multibyte character sets. You shouldn't use MySQLdb.escape_string.

In any case, you are much better off using parameterised queries as in Alex's answer. This makes it easier to port to other databases (it's part of the DB-API standard, which escape_string is not), and it's generally more convenient. (And much more convenient than parameterisation is in PHP.)

bobince