Help us understand the problem. What is going on with this article?

PythonのMySQLdbでUTF-8の日本語文字を扱う。

More than 3 years have passed since last update.

PythonでMySQLdbを通して日本語が含まれるレコードをぶっこ抜くのにunicodeになりやがる。

Python + MySQLで様々にググっても、たぶん簡単すぎて誰も書いてやがらないのでメモります。ポイントはconnector.cursorclass = MySQLdb.cursors.DictCursorのところなんだろうと思う。

sample.py
# -*- coding: utf-8 -*- 
# coding: UTF-8

import MySQLdb
from MySQLdb.cursors import DictCursor
if __name__ == "__main__":
    connector = MySQLdb.connect(host='localhost',db="dbname",user="user",passwd="password", charset="utf8")
    connector.cursorclass = MySQLdb.cursors.DictCursor
    cursor = connector.cursor()
    cursor.execute("SET NAMES utf8")
    cursor.execute = connector.cursor()
    cursor.execute.execute('SELECT * FROM sample limit 10')
    res = cursor.execute.fetchall()
    for row in res:
     print row['users']
    cursor.close()
    connector.close()
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした