llric
llric

Reputation: 13

AttributeError: 'NoneType' object has no attribute 'encoding' using pymysql

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()

having set charset but it is not useful,how can i insert Chinese into mysql

Upvotes: 0

Views: 6803

Answers (1)

tdelaney
tdelaney

Reputation: 77347

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")

Upvotes: 6

Related Questions