Python相当于mysql_real_escape_string,用于将字符串安全地导入到MySQL中?

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:http://stackoverflow.com/questions/2561178/python-equivalent-of-mysql-real-escape-string-for-getting-strings-safely-into-m

PHP的mysql_real_escape_string是否有 Python 等价物?

我正在尝试从Python将一些字符串插入到 MySQL 数据库中,并且不断地被字符串中的引号引起.

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)

我尝试过re.escape(),但转义字符串中的每个非字母数字字符,这不是我需要的 – 我只需要在这个实例中避免单引号).

可以手动做这个我猜,但是在Python中有更聪明的方法吗?

如果你使用的是mysql-python,那么试试看

MySQLdb.escape_string(SQL)

>>> import MySQLdb
>>> MySQLdb.escape_string("'")
"\\'"

http://stackoverflow.com/questions/2561178/python-equivalent-of-mysql-real-escape-string-for-getting-strings-safely-into-m


以上所述就是小编给大家介绍的《Python相当于mysql_real_escape_string,用于将字符串安全地导入到MySQL中?》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Realm of Racket

Realm of Racket

Matthias Felleisen、Conrad Barski M.D.、David Van Horn、Eight Students Northeastern University of / No Starch Press / 2013-6-25 / USD 39.95

Racket is the noble descendant of Lisp, a programming language renowned for its elegance and power. But while Racket retains the functional goodness of Lisp that makes programming purists drool, it wa......一起来看看 《Realm of Racket》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换