python编码mysql :( [英] python encoding mysql :(
本文介绍了python编码mysql :(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下python脚本(tes.py):
#!/ usr / bin / env python
# - * - 编码:utf-8 - * -
import MySQLdb
query =INSERT INTO test(test)VALUES('ñ')
print query +\\\
conn = MySQLdb.connect(host =localhost,user =ibrick,passwd =x,db =ibrick,charset = utf8)
conn.names =utf8
cursor = conn.cursor()
cursor.execute(query);
cursor.close()
conn.commit()
文件编码utf -8:
$ file -i tes.py
tes.py:text / x-java charset = utf -8
系统编码UTF:
#locale
LANG = es_AR.UTF-8
LC_CTYPE =es_AR.UTF-8
LC_NUMERIC =es_AR.UTF-8
LC_TIME =es_AR.UTF-8
LC_COLLATE =es_AR.UTF-8
LC_MONETARY =es_AR.UTF-8
LC_MESSAGES =es_AR.UTF-8
LC_PAPER =es_AR.UTF-8
LC_NAME =es_AR.UTF-8
LC_ADDRESS =es_AR.UTF-8
LC_TELEPHONE =es_AR.UTF- 8
LC_MEASUREMENT =es_AR.UTF-8
LC_IDENTIFICATION =es_AR.UTF-8
LC_ALL =
echoñññ> /tmp/test.txt
文件/tmp/test.txt
/tmp/test.txt:UTF-8 Unicode文本
MYSql表编码UTF8:
mysql>显示创建表测试;
+ ------- + ------------------------------------- -------------------------------------------------- ------- +
|表|创建表|
+ ------- + ------------------------------------- -------------------------------------------------- ------- +
|测试| CREATE TABLE`test`(
`test` varchar(10)default NULL
)ENGINE = MyISAM DEFAULT CHARSET = utf8 |
+ ------- + ------------------------------------- -------------------------------------------------- ------- +
控制台外出可以:
#。/ tes.py
/ pre>
INSERT INTO test(test)VALUES('ñ')
问题:
该脚本不会在表中插入ñ它插入一个坏字符:
select * from test;
+ ------ +
|测试|
+ ------ +
| |
| |
| |
| |
| |
| |
| |
+ ------ +
7行集(0.00秒)
任何人帮助我?
提前感谢
解决方案p>尝试添加'use_unicode'。
秘诀在于添加一个charset =utf8到你的连接
参数,而use_unicode = True。 来源
db = MySQLdb.connect(host = DB_HOST,user = DB_USER,passwd = DB_PASS,db = DB_NAME,charset =utf8,use_unicode = True)
I have the follow python script (tes.py):
#!/usr/bin/env python # -*- coding: utf-8 -*- import MySQLdb query = "INSERT INTO test(test) VALUES ('ñ')" print query + "\n" conn = MySQLdb.connect (host = "localhost", user = "ibrick", passwd = "x", db = "ibrick", charset="utf8") conn.names="utf8" cursor = conn.cursor() cursor.execute (query); cursor.close () conn.commit ()
file encoding utf-8:
$ file -i tes.py tes.py: text/x-java charset=utf-8
system encoding UTF:
#locale LANG=es_AR.UTF-8 LC_CTYPE="es_AR.UTF-8" LC_NUMERIC="es_AR.UTF-8" LC_TIME="es_AR.UTF-8" LC_COLLATE="es_AR.UTF-8" LC_MONETARY="es_AR.UTF-8" LC_MESSAGES="es_AR.UTF-8" LC_PAPER="es_AR.UTF-8" LC_NAME="es_AR.UTF-8" LC_ADDRESS="es_AR.UTF-8" LC_TELEPHONE="es_AR.UTF-8" LC_MEASUREMENT="es_AR.UTF-8" LC_IDENTIFICATION="es_AR.UTF-8" LC_ALL= echo "ñññ" > /tmp/test.txt file /tmp/test.txt /tmp/test.txt: UTF-8 Unicode text
MYsql table encoding UTF8:
mysql> show create table test; +-------+----------------------------------------------------------------------------------------------+ | Table | Create Table | +-------+----------------------------------------------------------------------------------------------+ | test | CREATE TABLE `test` ( `test` varchar(10) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 | +-------+----------------------------------------------------------------------------------------------+
Console outupt is OK:
#./tes.py INSERT INTO test(test) VALUES ('ñ')
problem:
The script doesn't insert ñ in the table.. it inserts a bad character:
select * from test; +------+ | test | +------+ | � | | � | | � | | � | | � | | � | | � | +------+ 7 rows in set (0.00 sec)
Anybody help me??
Thank in advance!
解决方案Try adding 'use_unicode'.
The secret ingredient is to add a charset="utf8″ to your connection parameters, and use_unicode=True. Source
db = MySQLdb.connect(host=DB_HOST, user=DB_USER, passwd=DB_PASS, db=DB_NAME, charset="utf8", use_unicode=True)
这篇关于python编码mysql :(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文