![]() |
String concatenation in SQL update statement - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: String concatenation in SQL update statement (/thread-36484.html) |
String concatenation in SQL update statement - hammer - Feb-24-2022 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: RE: String concatenation in SQL update statement - ReggieDrax - Feb-24-2022 Hi, Does the string f'UPDATE cowTbl SET {fldSet} WHERE {criteria}' evaluate as you expect it to? Reggie RE: String concatenation in SQL update statement - ibreeden - Feb-24-2022 (Feb-24-2022, 03:22 PM)hammer Wrote: fldSet: 'loc=?,aquired=?,disposition=?'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) RE: String concatenation in SQL update statement - hammer - Feb-24-2022 ReggieDrax, Yes the fString evaluates as expected. ibreeden, your recommendation solved the Exception. Thanks to all. |