ProgrammingError not all arguments converted during string formatting

0 votes

I am trying to insert a data into a BLOB column in MySQL Server it is keep giving me this error:

ProgrammingError: not all arguments converted during string formatting

I could not define why so please help,

P.S. the type of the column in MySQL is set to 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`

Sep 10, 2018 in Python by bug_seeker
• 15,510 points
20,276 views

1 answer to this question.

0 votes

Sorted!!!! just found the solution,

1 - apparently i could not use ? 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,

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()
answered Sep 10, 2018 by Priyaj
• 58,020 points
I had a very similar issue - the key was to put the data as represented by your variable named "contents" in brackets - [contents].

Admittedly, I am a novice python programmer - but I should have noticed what the execute function expected in the parameter list.

Related Questions In Python

0 votes
0 answers

TypeError: not all arguments converted during string formatting python

The program is supposed to take in ...READ MORE

Dec 22, 2022 in Python by erzan
• 630 points
1,059 views
0 votes
1 answer

Extract all characters of a string

Convert it to a list -  s = ...READ MORE

answered Jun 1, 2018 in Python by Nietzsche's daemon
• 4,260 points
834 views
+1 vote
2 answers

Python string formatting: % vs. .format

Using Python format() function is what the ...READ MORE

answered Apr 11, 2019 in Python by Dasa Ravi
1,397 views
+1 vote
2 answers

Remove all whitespace in a string in Python

You can also use regular expressions for ...READ MORE

answered Aug 31, 2018 in Python by Omkar
• 69,220 points
17,161 views
0 votes
1 answer

Create a database table using python

Hey @Kim, try something like this: import MySQLdb # ...READ MORE

answered Jul 23, 2019 in Python by Barbara
1,258 views
0 votes
1 answer

How to output text from database with line breaks in a django template?

Hello @kartik, Use linebreaks or linebreaksbr filter: {{ text|linebreaks }} Or surround the text ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
13,472 views
0 votes
1 answer

Error:Python3.4 can't install mysql-python

Hello @kartik, You can resolved this by the ...READ MORE

answered Jun 24, 2020 in Python by Niroj
• 82,840 points
2,121 views
0 votes
1 answer

ModuleNotFoundError: No module named 'mysql'

Hi@akhtar, You need to install mysql-connector to connect ...READ MORE

answered Jul 15, 2020 in Python by MD
• 95,460 points
50,254 views
0 votes
1 answer

ProgrammingError: not all arguments converted during string formatting

Sorted!!!! just found the solution, 1 - apparently ...READ MORE

answered Sep 20, 2018 in Python by Priyaj
• 58,020 points
5,685 views
0 votes
1 answer

Python string formatting: % vs. .format

To answer your first question... .format just ...READ MORE

answered Aug 17, 2018 in Python by Priyaj
• 58,020 points
995 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP