1
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

4
  • 3
    Please add the full backtrace that Python gives you. Also, please fix the formatting of the code, it's close to unreadable. Commented Apr 14, 2018 at 5:55
  • Does charset="utf8" work?
    – tdelaney
    Commented Apr 14, 2018 at 6:06
  • 1
    The charset is not the python encoding specifier but one cooked up by pymysql to map to encodings. You can find the full list at https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/charset.py
    – tdelaney
    Commented Apr 14, 2018 at 6:11
  • Its kinda lame that they don't catch the unknown charset and toss back a more sensible error message.
    – tdelaney
    Commented Apr 14, 2018 at 6:11

1 Answer 1

7

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.