My remote MySQL database and local MySQL database have the same table structure, and the remote and local MySQL database is utf-8
charset.
Asked
Active
Viewed 324 times
0

leon wu
- 315
- 3
- 10
-
What did you get if you insert those to mysql, and what displays in your table if you insert successfully. – KC. Nov 09 '18 at 09:02
-
@kcorlidy Hi, I edited my question, the sql is so simple, and my problem is not about the sql sentence. – leon wu Nov 09 '18 at 10:14
2 Answers
0
You'd better merge value and sql template string and print it , make sure the sql is correct.

winters
- 96
- 1
- 4
-
Hi, I edited my question, the sql is so simple, and my problem is not about the sql sentence. – leon wu Nov 09 '18 at 10:14
0
I know you are using python-2.x, but i am not sure what kinds of module you are importing. So i write so.
You only need to set charset="utf8" than you can write data successfully.
try:
import mysql.connector as mysql
except Exception as e:
import MySQLdb as mysql
connection = mysql.connect(user="k",passwd="k",db="k",charset="utf8")
mycursor = connection.cursor()
data = (222, u'yy', u'm', u'\u60a8\u597d'.encode("utf8"))
# data = (222, u'yy', u'm', u'\u60a8\u597d') #python-3.x
data = "insert into students (id_card, name, sex, order_info) values({}, '{}', '{}', '{}')".format(*data)
mycursor.execute(data)
connection.commit()
Reference Python & MySql: Unicode and Encoding

KC.
- 2,981
- 2
- 12
- 22