[英]ProgrammingError: not all arguments converted during string formatting
I am trying to insert a data into a BLOB column in MySQL
Server it is keep giving me this error:我正在尝试将数据插入
MySQL
服务器的 BLOB 列中,它一直给我这个错误:
ProgrammingError: not all arguments converted during string formatting
I could not define why so please help,我无法定义为什么所以请帮助,
PS the type of the column in MySQL is set to LONGBLOB PS MySQL中列的类型设置为LONGBLOB
here is my code:这是我的代码:
#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()
and the error:和错误:
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`
Sorted!!!!排序!!!! just found the solution,
刚刚找到解决方案,
1 - apparently i could not use ? 1 - 显然我不能使用? because of the format specifier, 2 - and i also did not add the con for not only being able to retrive but also to insert in the database,
由于格式说明符,2 - 我也没有添加 con 不仅可以检索而且还可以插入数据库,
here is the example of the code that worked for me:这是对我有用的代码示例:
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()
According to the Python Database Specification in PEP 249 , the format used in a query to show where to insert the parameters depends on the paramstyle
member of the database module:根据PEP 249 中的 Python 数据库规范,查询中用于显示参数插入位置的格式取决于数据库模块的
paramstyle
成员:
qmark
, use ?
qmark
,使用?
(question mark) numeric
, use :1
, :2
etc. (numeric, positional style)numeric
,使用:1
, :2
等(数字,位置样式)named
, use :name
(named style)named
,请使用:name
(命名样式)format
, use %s
(ANSI C printf format codes)format
,请使用%s
(ANSI C printf 格式代码)pyformat
, use %(name)s
(Python extended format codes)pyformat
,请使用%(name)s
(Python 扩展格式代码) AFAIR, MySQLdb uses format
, so you should replace your ?
AFAIR,MySQLdb 使用
format
,所以你应该替换你的?
with %s
.与
%s
。 (If MySQLdb would properly use prepared statements, it would be qmark
and ?
was the right way to go.) (如果 MySQLdb 会正确使用准备好的语句,那将是
qmark
并且?
是正确的方法。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.