We help IT Professionals succeed at work.

query = query % db.literal(args) TypeError: not enough arguments for format string

Dolamite Jenkins
on
I have to decrypt multiple columns

when I decrypt only one column it works fine
cursor.execute("SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s),First_Name, Age, Gender, Address,  City, State, Zip, Ailment, Treatment, Patient_reprt, Initial_contact,Hospital, Destination, Inservice, Provider_1, Provider_2, Sys, Dia,Pulse, Resp, Weather, Temp, Humid, Wind, TimeStamp, Refusal,image FROM ptrecords", key);
	

Open in new window


but when I decrypt multiple columns

cursor.execute("SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s), AES_DECRYPT(First_Name, %s),AES_DECRYPT(Age, %s),Gender, AES_DECRYPT(Address, %s),City,State,Zip,AES_DECRYPT(Ailment, %s),AES_DECRYPT(Treatment, %s),AES_DECRYPT(Patient_reprt, %s), Initial_contact,Hospital,AES_DECRYPT(Destination, %s),Inservice, Provider_1, Provider_2,AES_DECRYPT(Sys, %s),AES_DECRYPT(Dia, %s),AES_DECRYPT(Pulse, %s), AES_DECRYPT(Resp, %s), Weather, Temp, Humid, Wind, TimeStamp, Refusal, AES_DECRYPT(image, %s)FROM ptrecords", key);

Open in new window


I get
 File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 158, in execute
    query = query % db.literal(args)
TypeError: not enough arguments for format string

Open in new window


I dont see any errors but ... what's going on here ? what did I do wrong
      


Traceback (most recent call last):
  File "C:\Python26\lib\threading.py", line 532, in __bootstrap_inner
    self.run()
  File "C:\Python26\sign\MYSQL_NEW_SERVER_SIDE._II.py", line 6980, in run
    cursor.execute("SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s),AES_DECRYPT(First_Name, %s) , Age, Gender, Address,  City, State, Zip, Ailment, Treatment, Patient_reprt, Initial_contact,Hospital, Destination, Inservice, Provider_1, Provider_2, Sys, Dia,Pulse, Resp, Weather, Temp, Humid, Wind, TimeStamp, Refusal,image FROM ptrecords", key);
  File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 158, in execute
    query = query % db.literal(args)
TypeError: not enough arguments for format string

Open in new window

Comment
Watch Question

hoy are you defining "key" there? try something like
sql = "SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s), AES_DECRYPT(First_Name, %s),AES_DECRYPT(Age, %s),Gender, AES_DECRYPT(Address, %s),City,State,Zip,AES_DECRYPT(Ailment, %s),AES_DECRYPT(Treatment, %s),AES_DECRYPT(Patient_reprt, %s), Initial_contact,Hospital,AES_DECRYPT(Destination, %s),Inservice, Provider_1, Provider_2,AES_DECRYPT(Sys, %s),AES_DECRYPT(Dia, %s),AES_DECRYPT(Pulse, %s), AES_DECRYPT(Resp, %s), Weather, Temp, Humid, Wind, TimeStamp, Refusal, AES_DECRYPT(image, %s)FROM ptrecords"

keys = (col1_value, col2_value, ... and so on ....) 

cursor.execute(sql, keys)

Open in new window

In short.:

for every '%s' you need to provide one value.

you have multiple times '%s' in the query string, but you provide 'key' only once.

I am not really an SQL specialist and don't know by heart whether there are any specific shortcuts,
but the simplest one should be to change provide your key as often as you provde the '%s'

cursor.execute("SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s), AES_DECRYPT(First_Name, %s),AES_DECRYPT(Age, %s),Gender FROM ptrecords", (key, key, key));

# or alternatively
cursor.execute("SELECT Actual_Incident_number, Incident_number, AES_DECRYPT(Last_Name, %s), AES_DECRYPT(First_Name, %s),AES_DECRYPT(Age, %s),Gender FROM ptrecords", (key,) * 3 );

Open in new window

Author

Commented:
both solutions worked .. thank you both