我正在尝试将数据插入MySQL
服务器中的BLOB列,它一直给我这个错误:
ProgrammingError: not all arguments converted during string formatting
我无法定义原因,请帮助,
P.S。 MySQL中列的类型设置为LONGBLOB
这是我的代码:
#from mysql.connector import MySQLConnection, Error
import MySQLdb
def update_blob(filename):
# read file
pic = open(filename)
data = pic.read()
# prepare update query and data
query = "UPDATE image " \
"SET picture = ? "
print data
###############
hostname = ''
username = ''
password = ''
database = ''
try:
conn = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
print 'connected'
cursor = conn.cursor()
cursor.execute(query, data)
conn.commit()
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
和错误:
ProgrammingError Traceback (most recent call last)
<ipython-input-35-389eb7e8c3c0> in <module>()
----> 1 update_blob('hovik.jpg')
<ipython-input-34-48db763c9aee> in update_blob(filename)
21 print 'connected'
22 cursor = conn.cursor()
---> 23 cursor.execute(query, data)
24 conn.commit()
25 except Error as e:
>/usr/lib/python2.7/dist-packages/MySQLdb/cursors.pyc in execute(self, query, args)
238 query = query % args
239 except TypeError as m:
--> 240 self.errorhandler(self, ProgrammingError, str(m))
241
242 if isinstance(query, unicode):
>/usr/lib/python2.7/dist-packages/MySQLdb/connections.pyc in defaulterrorhandler(***failed resolving arguments***)
50 raise errorvalue
51 if errorclass is not None:
---> 52 raise errorclass(errorvalue)
53 else:
54 raise Exception(errorvalue)
`ProgrammingError: not all arguments converted during string formatting`
答案 0 :(得分:0)
排序!!!!刚刚找到解决方案,
1 - 显然我不能用?因为格式说明符, 2 - 我也没有添加con,因为它不仅能够进行检索,而且还可以插入数据库中,
以下是适用于我的代码示例:
import MySQLdb
hostname = ''
username = ''
password = ''
database = ''
myConnection = MySQLdb.connect( host=hostname, user=username, passwd=password, db=database )
def doQuery() :
fin = open("hovik.jpg",'rb')
contents = fin.read()
fin.close()
with myConnection:
cur = myConnection.cursor()
sql = "INSERT INTO image VALUES (%s)"
ret = cur.execute(sql, [contents])
doQuery()
myConnection.close()
答案 1 :(得分:0)
根据PEP 249中的Python数据库规范,查询中用于显示参数插入位置的格式取决于数据库模块的 @RequestMapping(value = "/parseHousePrice", method = RequestMethod.GET)
public String parseHousePrice(@Valid HousePrice housePriceObject, BindingResult bindingResult, Model model) {
// houseprice.jsp will open but obviously has none of the data
return "houseprice";
}
成员:
paramstyle
,请使用qmark
(问号)?
,请使用numeric
,:1
等(数字,位置风格):2
,请使用named
(命名样式):name
,请使用format
(ANSI C printf格式代码)%s
,请使用pyformat
(Python扩展格式代码) AFAIR,MySQLdb使用%(name)s
,因此您应将format
替换为?
。
(如果MySQLdb正确使用预准备语句,那么%s
和qmark
是正确的方法。)