Python MySQLdb TypeError: not all arguments converted during string formatting
Instead of this:
cur.execute( "SELECT * FROM records WHERE email LIKE '%s'", search )
Try this:
cur.execute( "SELECT * FROM records WHERE email LIKE %s", [search] )
See the MySQLdb documentation. The reasoning is that execute
's second parameter represents a list of the objects to be converted, because you could have an arbitrary number of objects in a parameterized query. In this case, you have only one, but it still needs to be an iterable (a tuple instead of a list would also be fine).
pymsql - TypeError: not all arguments converted during string formatting when passing multiple parameters
You are missing couple of %s
s. Correct statement:
c.execute('''INSERT INTO upcoming_events1 (name, html_insert, date, institution) VALUES (%s, %s, %s, %s);''', (header, my_html, event_date, event_institution, ))
ProgrammingError: not all arguments converted during string formatting
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()
Executing database query gives TypeError: not all arguments converted during string formatting
cursor.execute
expects a list as its second argument
q_list_all = "SELECT * FROM item_info WHERE item_code = '%s'"
print q_list_all
a_list_all = (value,) #the comma makes it a list not the parens
items_request = db.query(q_list_all, a_list_all)
when you pass it a string
cur.execute(qry,"MyArg") # =becomes==> cur.execute(qry,["M","y","A","r","g"])
on a somewhat unrelated note you should really think about just using an ORM like sqlalchemy
cx_Oracle: not all arguments converted during string formatting
Thanks to the pointer by @christopher-jones, I figured the problem out. (I originally thought the problem was the query, so I didn't dive into this before posting the question.)
I was using an (inhouse-written) middleware library that I thought was using cx_Oracle
for all database handling, but for the MariaDB
case it was using pymysql
instead (which makes sense, of course).
And apparently, pymysql
doesn't do positional binding the way cx_Oracle
does but uses %s placeholders instead.
So I changed the query to:
query = "UPDATE temp SET file_logging=%s, file_report=%s, submission_subfolder=%s, status=%s where id = 1"
and now it's working.
Related Topics
Partial Coloring of Text in Matplotlib
Alternative to Dict Comprehension Prior to Python 2.7
Python Creating a Dictionary of Lists
Compare Two Columns Using Pandas
Find Column Whose Name Contains a Specific String
Why Can Tuples Contain Mutable Items
Creating a Dynamic Choice Field
What Is the Purpose of a Backslash at the End of a Line
How to Convert the Background Color of Image to Match the Color of Pygame Window
Executing Multi-Line Statements in the One-Line Command-Line
How to Install Both Python 2.X and Python 3.X in Windows
Differencebetween Flatten and Ravel Functions in Numpy
Postgresql: How to Install Plpythonu Extension
How to Find Script's Directory
Priority of the Logical Operators Not, And, or in Python
How to Allow or Deny Notification Geo-Location Microphone Camera Pop Up