【发布时间】:2013-03-19 15:53:23
【问题描述】:
我想将包含二进制数据的字符串对象插入 MySQL blob 列。但是,我不断收到 MySQL 语法错误。
我为调试目的制作了一个小脚本:
import MySQLdb
import array
import random
conn = MySQLdb.connect(host="localhost", user="u", passwd="p", db="cheese")
cur = conn.cursor()
a = array.array('f')
for n in range(1,5):
a.append(random.random())
bd = a.tostring()
print type(bd) # gives str
query = '''INSERT INTO cheese (data) VALUES (%s)''' % bd
cur.execute(query)
结果(但不是每次都...)
ProgrammingError: (1064, "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 '?fJ\x95<=
\xad9>\xf3\xec\xe5>)' at line 1")
问题显然归结为 MySQL 不喜欢的二进制数据中的某些字符。是否有将二进制数据放入 MySQL 数据库的安全方法?
【问题讨论】: