查看python shell中的代码:
>>> s = u'赵孟頫'.encode('gbk')
>>> s
'\xd5\xd4\xc3\xcf\xee\\'
'赵孟俯'的最后一个字节是\ x5c,与反斜杠相同。它会导致sql错误。
mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''?????\\')' at line 4
这是我的代码:
# db is mysql.connector object
sql = '''
INSERT INTO scraped_products(
site_prd_id,site_id,brand)
VALUES(
%(site_prd_id)s,%(site_id)s,%(brand)s)
'''
dat = {
'site_prd_id' : 'test',
'site_id' : 1,
'brand' : u'赵孟頫'.encode('gbk'),
}
self.db.ping(True, 3, 1)
self.db.cursor().execute(sql, dat)
答案 0 :(得分:2)
我有一个解决方案,需要一些额外的工作才能让它正常工作。下面的代码示例是将数据转换为MySQL Hexadecimal Literal并将其发送到MySQL而不转义,引用或转换它。这有点不同的执行查询的方式,但我希望它现在可以服务:
import mysql.connector
cnx = mysql.connector.connect(database='test', user='root',
charset='gbk', use_unicode=False)
cur = cnx.cursor()
cur.execute("DROP TABLE IF EXISTS gbktest")
table = (
"CREATE TABLE gbktest ("
"id INT AUTO_INCREMENT KEY, "
"c1 VARCHAR(40)"
") CHARACTER SET 'gbk'"
)
cur.execute(table)
def gbk_to_hexstr(value):
"""Convert value to Hexadecimal Literal for MySQL
"""
return "0x{0}".format(''.join(
["{0:x}".format(ord(c)) for c in value.encode('gbk')]))
# Convert your Unicode data using gbk_to_hexstr
data = {
'c1' : gbk_to_hexstr(u'赵孟頫'),
}
# Use MySQLCursor.execute() _not_ passing data as second argument
cur.execute("INSERT INTO gbktest (c1) VALUES ({c1})".format(**data))
cur.execute("SELECT c1 FROM gbktest")
# Print the inserted data as Unicode
for row in cur:
print(row[0].decode('gbk').encode('utf8'))