Python Forum

Full Version: String concatenation in SQL update statement
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
In the following Update statement i get shown Exception. Below are the arguments.
Could it be the parentheses around argg that is causing issue?
If so, i have tried .strip() without success.

fldSet: 'loc=?,aquired=?,disposition=?'
criteria: 'tagNo=? and tagClr=? and tagYr=?'
argg: 'Hermas,Purchased,Died,22,Red,2014'


 def global_cowUpDte(self,fldSet,criteria,argg):
        self.c.execute (f'UPDATE cowTbl SET {fldSet} WHERE {criteria}',(argg))

Exception has occurred: ProgrammingError
Incorrect number of bindings supplied. The current statement uses 6, and there are 35 supplied.
Exception has occurred:
Hi,

Does the string f'UPDATE cowTbl SET {fldSet} WHERE {criteria}' evaluate as you expect it to?

Reggie
(Feb-24-2022, 03:22 PM)hammer Wrote: [ -> ]fldSet: 'loc=?,aquired=?,disposition=?'
criteria: 'tagNo=? and tagClr=? and tagYr=?'
argg: 'Hermas,Purchased,Died,22,Red,2014'
...
The current statement uses 6, and there are 35 supplied.
6 bindings is correct, there are 6 placeholders (question marks) in "fldSet" and "criteria". But "argg" seems to have 35 values. It must be the number of characters. "argg" must be a tuple but it arrives as a string parameter and you think you can force it to be a tuple by putting parentheses around it. That does not work. Try this:
values = tuple(argg.split(","))
self.c.execute (f'UPDATE cowTbl SET {fldSet} WHERE {criteria}', values)
ReggieDrax, Yes the fString evaluates as expected.
ibreeden, your recommendation solved the Exception.

Thanks to all.