AttributeError: 'NoneType' 对象没有使用 pymysql 的属性 'encoding' [英] AttributeError: 'NoneType' object has no attribute 'encoding' using pymysql

查看:60
本文介绍了AttributeError: 'NoneType' 对象没有使用 pymysql 的属性 'encoding'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',
    passwd='123456',db='home', charset="utf-8")

cursor = conn.cursor()
cursor.execute("""create table job_list(job varchar(30) , people varchar(30) , catagory varchar(30)  , place varchar(30), publish varchar(30)) """)

try:
    cursor.execute("""INSERT  INTO  job_list(job,people,catagory,place,publish) VALUES (%s, %s, %s, %s, %s)""",
        ["算法工程师", "2018毕业生", "研发", "雅加达", "2018-03-28"])
    conn.commit()
except pymysql.Error as e:
    print(e)

cursor.close()
conn.close()

设置了字符集但是没有用,如何在mysql中插入中文

推荐答案

charset 是需要转换为python编码的mysql数据库字符集名称.pymysqlcharset 中有一个很大的列表.py.相反,pymysql 要么吐回您传入的字符集名称,要么引发您看到的非明显错误.在 pymysql 世界中,utf8"是有效的字符集,但utf-8"不是.所以,只需将您的连接更改为

charset is a mysql database character set name that needs to be converted to a python encoding. pymysql has a large list of them in charset.py. Rather perversely, pymysql either spits back the charset name you passed in or raises the non-obvious error you see. In the pymysql world, "utf8" is a valid character set, but "utf-8" is not. So, just change your connect to

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',
    passwd='123456',db='home', charset="utf8")

这篇关于AttributeError: 'NoneType' 对象没有使用 pymysql 的属性 'encoding'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆