2

使用 python 2.7.5。所有数据库和表都是

在此处输入图像描述

我的代码如下所示:

import MySQLdb as mdb
import urllib2
import sys
import logging
logging.basicConfig(level=logging.INFO)

from bs4 import BeautifulSoup as BS
con = mdb.connect('loclhost', 'root', '', 'mydb');
cur = con.cursor()
cur.execute('SET NAMES utf8;')
cur.execute('SET CHARACTER SET utf8;')
cur.execute('SET character_set_connection=utf8;')
with con:
...
        sql_insert = """INSERT INTO Teams (name, category, countryId) VALUES (%s, 1, %s)"""
        cursor = con.cursor()
        try:
            affected_count = cursor.execute(sql_insert, (name, id))  <<< this line
            con.commit()
        except mdb.IntegrityError:
            logging.warn("failed to insert values %s, %s", name, id)
        finally:
           cursor.close()
...

con.close()

收到错误信息:

“UnicodeEncodeError:'latin-1' 编解码器无法在位置 2 编码字符 u'\u015f':序数不在范围内(256)”

上面标记的线。我究竟做错了什么?

4

1 回答 1

9

尝试:

con = mdb.connect('loclhost', 'root', '', 'mydb', 
                  use_unicode=True, charset='utf8')

这是一个演示它的工作原理:

如果您不使用use_unicode=True以下设置,则会收到 UnicodeEncodeError:

import MySQLdb
import config

def setup_charset(cursor, typ='latin1'):
    sql = 'DROP TABLE IF EXISTS foo'
    cursor.execute(sql)
    sql = '''\
        CREATE TABLE `foo` (
          `fooid` int(11) NOT NULL AUTO_INCREMENT,
          `bar` varchar(30),
          `baz` varchar(30),
          PRIMARY KEY (`fooid`)) DEFAULT CHARSET={t}
        '''.format(t=typ)
    cursor.execute(sql)
    sql = 'INSERT INTO foo (bar,baz) VALUES (%s,%s)'

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER,
    passwd=config.PASS, db='test')

cursor = connection.cursor()
setup_charset(cursor, typ='utf8')
sql = u'INSERT INTO foo (bar,baz) VALUES (%s,%s)'
try:
    cursor.execute(sql, [u'José Beiträge', u'∞'])
except UnicodeEncodeError as err:
    # You get this error if you don't use
    # (use_unicode=True, charset='utf8') see below.
    print(err)

引发异常:

'latin-1' codec can't encode character u'\u221e' in position 0: ordinal not in range(256)

同时,如果您确实使用use_unicode=True,则可以插入 unicode 而不会出错:

connection = MySQLdb.connect(
    host=config.HOST, user=config.USER,
    passwd=config.PASS, db='test',
    use_unicode=True,
    charset='utf8')
cursor = connection.cursor()
cursor.execute(sql, ['José Beiträge', '∞'])
cursor.execute('SELECT * from foo')
for row in cursor:
    print(u'{} {}'.format(*row[1:]))

印刷

José Beiträge ∞
于 2013-09-06T17:10:15.153 回答