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
答案 0 :(得分:3)
charset
是一个需要转换为python编码的mysql数据库字符集名称。 pymysql
在charset.py中有一个很大的列表。相反,pymysql要么回吐你传入的字符集名称,要么引发你看到的非显而易见的错误。在pymysql
世界中,“utf8”是有效的字符集,但“utf-8”不是。所以,只需将连接更改为
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',
passwd='123456',db='home', charset="utf8")