1

查看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)
4

1 回答 1

2

我有一个解决方案,需要一些额外的工作才能使其正常工作。以下代码示例将数据转换为MySQL 十六进制文字并将其发送到 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'))
于 2013-07-15T12:04:13.453 回答