0
我正在構建一個擴展unicode類和其他一些方法的庫,我發現MySQLdb在使用我的unicode類時失敗。MySQLdb execute()適用於unicode,但不適用於unicode的子類
這工作:
In [23]: c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(u'éric',))
Out[23]: 0L
但是,這並不:
In [24]: class UnicodeExtended(unicode):
...: pass
...:
In [25]: c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(UnicodeExtended(u'éric'),))
---------------------------------------------------------------------------
UnicodeEncodeError Traceback (most recent call last)
<ipython-input-25-9145669d2b00> in <module>()
----> 1 c.execute("""SELECT * FROM django_site WHERE domain LIKE %s""",(UnicodeExtended(u'éric'),))
/usr/lib/python2.7/dist-packages/MySQLdb/cursors.pyc in execute(self, query, args)
205 args = dict((key, db.literal(item)) for key, item in args.items())
206 else:
--> 207 args = tuple(map(db.literal, args))
208 if not PY2 and isinstance(query, bytes):
209 query = query.decode(db.unicode_literal.charset)
/usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in literal(self, o)
302
303 """
--> 304 s = self.escape(o, self.encoders)
305 # Python 3 doesn't support % operation for bytes object.
306 # We should decode it before using %.
/usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in string_literal(obj, dummy)
213 # Note: string_literal() is called for bytes object on Python 3.
214 def string_literal(obj, dummy=None):
--> 215 return db.string_literal(obj)
216 return string_literal
217
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 0: ordinal not in range(128)
是否有可能使用unicode到SQL的sublass以任何方式查詢參數?
可能是我的UnicodeExtended類缺少的東西...任何想法?
林正在使用Python 2.7和1.3.7 MySQLdb的