导入mysqldb,将数据插入mysql,出错,请问能能怎么解决?? 400 报错
在python2.6下,连接数据库,插入数据时出错,出错信息:
File "/usr/lib/python2.6/site-packages/MySQL_python-1.2.3-py2.6-linux-i686.egg/MySQLdb/cursors.py", line 157, in execute query = query.encode(charset) UnicodeEncodeError: 'latin-1' codec can't encode characters in position 32-33: ordinal not in range(256)
请问该怎么解决。
数据建表:
create table user( id int(4) not null primary key auto_increment, name char(20))DEFAULT CHARSET=utf8;
python 语句:
insert_sql = 'insert into user (name) values(\'%s\')'%myself.name cur.execute(insert_sql)
connect的时候指定charset=utf8
下面那句insert最好改成
insert_sql = 'insert into user (name) values(\'%s\')'
cur.execute(insert_sql,( myself.name,))
######感谢!connect时需指定编码######数据库要用UTF8编码哦,你似乎没有######数据库编码:
my.chf设置:
[mysqld] datadir=/home/mysql/data socket=/home/mysql/server/mysql.sock user=mysql # Disabling symbolic-links is recommended to prevent assorted security risks symbolic-links=0 default-character-set=utf8 [mysqld_safe] log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid [mysql] socket=/home/mysql/server/mysql.sock [client] socket=/home/mysql/server/mysql.sock default-character-set=utf8
######cur.execute("insert into user (name) values (%s)", [myself .name])
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。