Создание таблицы:
mysql> create table names (name text);
Python 2:
# -*- coding: utf-8 -*-
import MySQLdb
db = MySQLdb.connect('localhost', msql_user_name, msql_user_pass, 'vstat')
cursor = db.cursor()
insert = "INSERT INTO names (name) VALUES ('%s')" % 'абв'
cursor.execute(insert)
db.commit()
db.close()
mysql> SELECT * FROM names;
+--------------+
| name |
+--------------+
| абв |
+--------------+
При добавлении:
db.set_character_set('utf8')
cursor.execute('SET NAMES utf8;')
cursor.execute('SET CHARACTER SET utf8;')
cursor.execute('SET character_set_connection=utf8')
Вылетает ошибка:
Warning: Incorrect string value: '\xD0\xB0\xD0\xB1\xD0\xB2' for column 'name' at row 1
cursor.execute(insert)
и вывод такой:
mysql> SELECT * FROM names;
+------+
| name |
+------+
| ??? |
+------+