Handles UTF-8 Japanese characters in Python's MySQLdb.

It tends to be unicode to pull out records containing Japanese through MySQLdb in Python.

Even if you google variously with Python + MySQL, it's probably too easy for anyone to write, so make a note. I think the point is 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()

Recommended Posts

Handles UTF-8 Japanese characters in Python's MySQLdb.
Avoid UnicodeEncodeError when throwing queries with Japanese in Python's MySQLdb
Create an image with characters in python (Japanese)
Iterator in MySQLdb
Fix garbled characters when handling Japanese in Requests
Japanese output in Python
English PDF in Japanese
Eliminate garbled Japanese characters in JSON data acquired by API.
Eliminate garbled Japanese characters in Python library matplotlib and NetworkX