它 是在代码页1252(西欧)中出现。这是基于 ISO-8859-1的特定于 Windows 的编码,但是它将额外的字符放入范围0x80-0x9F 中。代码页1252经常与 ISO-8859-1混淆,这是一种恼人但现在已经成为标准的网页浏览器行为,如果你将你的页面设置为 ISO-8859-1,浏览器将把它们设置为 cp1252。然而,它们实际上是两种截然不同的编码方式:
>>> u'He said \u201CHello\u201D'.encode('iso-8859-1')
UnicodeEncodeError
>>> u'He said \u201CHello\u201D'.encode('cp1252')
'He said \x93Hello\x94'
If you are using your database only as a byte store, you can use cp1252 to encode “ and other characters present in the Windows Western code page. But still other Unicode characters which are not present in cp1252 will cause errors.
您可以使用 encode(..., 'ignore')通过去除字符来抑制错误,但实际上在本世纪,您应该在数据库和页面中都使用 UTF-8。这种编码允许使用任何字符。理想情况下,您还应该告诉 MySQL 您正在使用 UTF-8字符串(通过设置数据库连接和字符串列上的排序规则) ,这样它就可以获得不区分大小写的比较和排序。
Python: 您需要添加
#-*-编码: UTF-8-*-(删除周围的空格 *)
to the first line of the python file. and then add the following to the text to encode: .encode('ascii', 'xmlcharrefreplace'). This will replace all the unicode characters with it's ASCII equivalent.
Db = MySQLdb.connect (host = “ localhost”,user = “ root”,passwd = “”,db = “ testdb”,use _ unicode = True,charset = “ utf8”)-KyungHoon Kim Mar
13分14秒17分04秒
详见:
class Connection(_mysql.connection):
"""MySQL Database Connection Object"""
default_cursor = cursors.Cursor
def __init__(self, *args, **kwargs):
"""
Create a connection to the database. It is strongly recommended
that you only use keyword parameters. Consult the MySQL C API
documentation for more information.
host
string, host to connect
user
string, user to connect as
passwd
string, password to use
db
string, database to use
port
integer, TCP/IP port to connect to
unix_socket
string, location of unix_socket to use
conv
conversion dictionary, see MySQLdb.converters
connect_timeout
number of seconds to wait before the connection attempt
fails.
compress
if set, compression is enabled
named_pipe
if set, a named pipe is used to connect (Windows only)
init_command
command which is run once the connection is created
read_default_file
file from which default client values are read
read_default_group
configuration group to use from the default file
cursorclass
class object, used to create cursors (keyword only)
use_unicode
If True, text-like columns are returned as unicode objects
using the connection's character set. Otherwise, text-like
columns are returned as strings. columns are returned as
normal strings. Unicode objects will always be encoded to
the connection's character set regardless of this setting.
charset
If supplied, the connection character set will be changed
to this character set (MySQL-4.1 and newer). This implies
use_unicode=True.
sql_mode
If supplied, the session SQL mode will be changed to this
setting (MySQL-4.1 and newer). For more details and legal
values, see the MySQL documentation.
client_flag
integer, flags to use or 0
(see MySQL docs or constants/CLIENTS.py)
ssl
dictionary or mapping, contains SSL connection parameters;
see the MySQL documentation for more details
(mysql_ssl_set()). If this is set, and the client does not
support SSL, NotSupportedError will be raised.
local_infile
integer, non-zero enables LOAD LOCAL INFILE; zero disables
autocommit
If False (default), autocommit is disabled.
If True, autocommit is enabled.
If None, autocommit isn't set and server default is used.
There are a number of undocumented, non-standard methods. See the
documentation for the MySQL C API for some hints on what they do.
"""