python使用MySQLdb,往库表内添加中文字段要做的修改

来源:互联网 发布:iphone离线看书软件 编辑:程序博客网 时间:2024/06/05 02:55

1,配置mysql库属性不重要


2,代码中要进行额外的编码转换配置(sqlite不需要,因为sqlite默认编码就是unicode,和python内部一致)

开头:

reload(sys)
sys.setdefaultencoding('utf8')


连接库表时:

    cx=MySQLdb.connect(db='',user='',passwd='',host='localhost')
    cx.set_character_set('utf8')


执行添加sql之前:

    cu=cx.cursor()
    cu.execute('set names utf8;')
    cu.execute('set character set utf8;')
    cu.execute('set character_set_connection=utf8;')

0 0
原创粉丝点击