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