2

私が使用するもの: Python2.7 / MySQLdb 1.2.3

MySQLdb.cursors を使用して実行するINSERT IGNORE INTO reporter('张三', '2013-11-11'), ('张三', '2013-11-11')と、
このような UnicodeEncodeError エラーが発生し、警告を表示すると発生します

File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/cursors.py", line 224, in executemany
    if not self._defer_warnings: self._warning_check()
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/cursors.py", line 92, in _warning_check
    warn(w[-1], self.Warning, 3)
  File "/usr/lib/python2.7/warnings.py", line 29, in _show_warning
    file.write(formatwarning(message, category, filename, lineno, line))
  File "/usr/lib/python2.7/warnings.py", line 38, in formatwarning
    s =  "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-18: ordinal not in range(128)

私は何をすべきか?

これは私のコードです:

# -*- coding: utf-8 -*-
import MySQLdb

db_conn = MySQLdb.connect(
    host='localhost', user='root', passwd='', charset='utf8', db='test')
cursor = db_conn.cursor()
cursor.executemany(
    'INSERT IGNORE INTO unicode_test values(%s, %s)',
    [('张三', '2013-11-11'), ('张三', '2013-11-11')])
db_conn.commit()
cursor.close()
db_conn.close()

それが来る

Traceback (most recent call last):
  File "/home/cooper/Document/20131106.py", line 9, in <module>
    [('张三', '2013-11-11'), ('张三', '2013-11-11')])
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/cursors.py", line 224, in executemany
    if not self._defer_warnings: self._warning_check()
  File "/usr/local/lib/python2.7/dist-packages/MySQL_python-1.2.3-py2.7-linux-i686.egg/MySQLdb/cursors.py", line 92, in _warning_check
    warn(w[-1], self.Warning, 3)
  File "/usr/lib/python2.7/warnings.py", line 29, in _show_warning
    file.write(formatwarning(message, category, filename, lineno, line))
  File "/usr/lib/python2.7/warnings.py", line 38, in formatwarning
    s =  "%s:%s: %s: %s\n" % (filename, lineno, category.__name__, message)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 17-18: ordinal not in range(128)
[Finished in 0.2s with exit code 1]

これは、MariaDB 10.0 で使用するテーブルです。

CREATE TABLE `unicode_test` (
 `name` varchar(10) NOT NULL,
 `date_of` date NOT NULL,
 PRIMARY KEY (`name`,`date_of`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
4

2 に答える 2

4

最後に、私は手がかりを持っているようです。Unicode エラーが MySQL ではなく警告で発生するという重要なポイントをトレースで見逃したようです。

これを取り除くには、MySQLdb の警告をフィルタリングして追加します。

warnings.filterwarnings('ignore', category=MySQLdb.Warning)

あなたのスクリプトに。私のインストールでは、警告が正確に何であるかわかりません。mysqlコンソールでクエリを手動で実行し、SHOW WARNINGS;その後実行して確認できます。

または、 から関連する関数にパッチをwarnings.py適用して、ASCII 以外のエンコーディングに耐性を持たせ、Python でチェックアウトすることもできます。

于 2013-11-06T21:00:42.700 に答える
0

テーブル/MySQL パラメータを表示します。ラップトップでこれを実行できます (cp1250 でエンコードされた端末のため、文字が異なります):

>>> MySQLdb.__version__
'1.2.3'
>>> sys.version
'2.7.2 (default, Aug 15 2012, 16:03:20) \n[GCC 4.4.3]'

$ mysqldump --no-data -ualko test unicode_test
--
-- Table structure for table `unicode_test`
--
DROP TABLE IF EXISTS `unicode_test`;
CREATE TABLE `unicode_test` (
  `name` varchar(100) DEFAULT NULL,
  `date` varchar(100) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

$ python test.py
$ mysql -ualko test
mysql> select * from unicode_test;
+--------+------------+
| name   | date       |
+--------+------------+
| 张三   | 2013-11-11 |
| 张三   | 2013-11-11 |
+--------+------------+
2 rows in set (0.00 sec)

アップデート

接続設定は何ですか?

>>> import MySQLdb
>>> db_conn = MySQLdb.connect(**settings)
>>> cursor = db_conn.cursor()
>>> cursor.execute('select @@COLLATION_CONNECTION, '
...     '@@CHARACTER_SET_RESULTS, @@CHARACTER_SET_CLIENT')
1L
>>> cursor.fetchone()
(u'utf8_general_ci', u'utf8', u'utf8')
于 2013-11-06T08:50:18.003 に答える