问题描述
在Python模块mysqldb中有很多转义函数,我的文档不明白,
>>> print _mysql.escape .__ doc__
escape(obj,dict) - 使用映射dict转义对象obj
中的任何特殊字符,为每种类型提供引用功能。
返回一个SQL文字字符串。
说同样的话。但是,映射字典应该是什么呢?我尝试了几件(大部分是随机的)东西,只有出现错误。更令人沮丧的是,虽然 escape_string()
方法有效,但其文档字符串是:
>>> print _mysql.escape_string .__ doc__
escape_string(s) - 引用字符串s中的任何SQL解释字符。
如果您使用它,请使用connection.escape_string(s)。
_mysql.escape_string(s)无法处理字符集。你是
可能会更好地使用connection.escape(o),因为
它将转义整个序列以及字符串。
所以,我最好使用 _mysql.escape()
,我是吗?嗯,嗯...好吧,但是怎么样? 映射字典是什么?至少这个PHP的这个方法呢是不太神秘的。
我通过查看/ usr / lib / pymodules / python2.6 / MySQLdb / connections.py
查看它如何调用 connection.escape
。一点点嗅觉导致
MySQLdb.converters.conversions
。这是一个代码片段:
{0:< class'decimal.Decimal'>,
1:< ; type'int'>
...
< type'dict'> ;:<内置函数escape_dict>
< type'NoneType'> ;: <在0xae9717c>上的函数None2NULL,
< type'set'> ;:< function在SetClass2Str在0xae9709c>,
< type'str'> ;:< function Thing2Literal at 0xae971b4>
< type'tuple'> ;:<内置函数escape_sequence>
< type'object'> ;:< function Instance2Str at 0xae971ec>
< type 'unicode'> ;:< function Unicode2Str at 0xae9710c>
< type'array.array'> ;:< function array2Str at 0xae9725c>
< type'bool'> ;: < function bool2Str at 0xae97294>}
您可以这样使用:
import MySQLdb
import MySQLdb.converters
import datetime
now = datetime.datetime.now( )
conn ection = MySQLdb.connect(
host = HOST,user = USER,passwd = PASS,db = MYDB)
print(connection.escape((1,2,now),MySQLdb.converters.conversions) )
#('1','2',''2010-07-24 19:33:59')
PS。关于Bobby表:对于MySQLdb的正常使用,您不必手动转义参数。调用 cursor.execute
时使用参数化参数,MySQLdb会自动引用您的参数。
例如:
sql ='insert into students ,等级,日期)值(%s,%s,%s)'
args =(Robert); DROP TABLE学生; - ,60,现在)#不需要手动报价
cursor = connection.cursor()
cursor.execute(sql,args)
There are many escape functions in the Python module mysqldb whose documentation I don't understand, and my efforts at looking them up have revealed nothing.
>>> print _mysql.escape.__doc__
escape(obj, dict) -- escape any special characters in object obj
using mapping dict to provide quoting functions for each type.
Returns a SQL literal string.
This documentation page says the same thing. But what's supposed to be in that "mapping dict"? I tried a couple of (mostly random) things and only go errors back. What's even more frustrating is that, while the escape_string()
method works, its documentation string is:
>>> print _mysql.escape_string.__doc__
escape_string(s) -- quote any SQL-interpreted characters in string s.
Use connection.escape_string(s), if you use it at all.
_mysql.escape_string(s) cannot handle character sets. You are
probably better off using connection.escape(o) instead, since
it will escape entire sequences as well as strings.
So, I am better off using _mysql.escape()
, am I? Well, uh... okay, but how? What on earth is that "mapping dict"? PHP, in that way at least, was a lot less cryptic.
I learned this by looking in /usr/lib/pymodules/python2.6/MySQLdb/connections.pyto see how it called connection.escape
. A little sniffing around leads to MySQLdb.converters.conversions
. Here is a snippet:
{0: <class 'decimal.Decimal'>,
1: <type 'int'>,
...
<type 'dict'>: <built-in function escape_dict>,
<type 'NoneType'>: <function None2NULL at 0xae9717c>,
<type 'set'>: <function Set2Str at 0xae9709c>,
<type 'str'>: <function Thing2Literal at 0xae971b4>,
<type 'tuple'>: <built-in function escape_sequence>,
<type 'object'>: <function Instance2Str at 0xae971ec>,
<type 'unicode'>: <function Unicode2Str at 0xae9710c>,
<type 'array.array'>: <function array2Str at 0xae9725c>,
<type 'bool'>: <function Bool2Str at 0xae97294>}
You can use it like this:
import MySQLdb
import MySQLdb.converters
import datetime
now=datetime.datetime.now()
connection=MySQLdb.connect(
host=HOST,user=USER,passwd=PASS,db=MYDB)
print(connection.escape((1,2,now),MySQLdb.converters.conversions))
# ('1', '2', "'2010-07-24 19:33:59'")
PS. Regarding Bobby Tables: For normal use of MySQLdb, you don't have to manually escape arguments. Just use parametrized arguments when calling cursor.execute
, and MySQLdb will automatically quote the arguments for you.
For example:
sql='insert into students (name,grade,date) values (%s, %s, %s)'
args=("Robert'); DROP TABLE Students; --",60,now) # no manual quotation necessary
cursor=connection.cursor()
cursor.execute(sql,args)
这篇关于Python的mysqldb模糊的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!