这是我的代码,它连接到数据库,并将俄语单词插入数据库:
import mysql.connector
if __name__ == '__main__':
try:
cnx = mysql.connector.connect(user='user', password='password',
host='123.123.123.123',
database='db_name',
charset='utf8',
collation='utf8_general_ci',
use_unicode=True)
except BaseException as ex:
print('Error: ' + str(ex))
cnx.close()
else:
print('Connections succesful!')
cnx.set_charset_collation('utf8', 'utf8_general_ci')
cursor = cnx.cursor()
print(cnx.charset)
cursor.execute("INSERT INTO table_name (column_name) values ('привет!')") #means 'Hello!' in russian
cursor.execute("SELECT * FROM test_referers")
print(cursor.fetchall())
这就是我得到的:
Connection succesful!
utf8
(1, u'\u043f\u0440\u0438\u0432\u0435\u0442!')
这样的输出出现在 Python 解释器控制台和 Eclipse PyDev 中。
请帮助