Python Forum
How do you format Update statement with multiple conditions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do you format Update statement with multiple conditions
#1
Below shows a workableUpdate format for changing 1 field with 1 Where condition. I understand how the Bobby Table Format association works here.
What would the Format look like with multiple field changes and multiple conditions?

def update_cowTbl(self,tagNoC,tagClrC,tagYrC,tagNoV,tagClrV,tagYrV):
self.c.execute("UPDATE cowTbl SET tagNo=? WHERE tagNo=?",(tagNoV,tagNoC))
self.con.commit()

What does not work: ("UPDATE cowTbl SET tagNo=?,tagClr=? WHERE tagNo=?,tagClr=?",(tagNoV,tagNoC),(tagClrV,tagClrC))
Larz60+ write Dec-16-2021, 04:42 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.

Please use bbcode tags, it's a forum rule.
Reply
#2
(Dec-16-2021, 02:13 AM)hammer Wrote: What does not work: ("UPDATE cowTbl SET tagNo=?,tagClr=? WHERE tagNo=?,tagClr=?",(tagNoV,tagNoC),(tagClrV,tagClrC))
There should be one tuple, and multiple "where" conditions should be separated by "and" or "or". So:
"UPDATE cowTbl SET tagNo=?, tagClr=? WHERE tagNo=? AND tagClr=?",(tagNoV, tagNoC, tagClrV, tagClrC)
Reply
#3
Thank you.
I made just one tuple and i added the "and"' for separating the conditions as you suggested. It does not error-out like before but I can not seem to get the items in the tuple sequenced correctly to return what needs to be updated. Could you go a little bit further with what in the tuple matches up with what in the SET and WHERE using below-ie val1-?1, val2-?4 etc?

"UPDATE cowTbl SET A=?1, B=?2, C=?3 Where X=?4 and X=?5" ,(val1, val2, val3,val4,val5)
Reply
#4
(Dec-16-2021, 02:50 PM)hammer Wrote: Could you go a little bit further with what in the tuple matches up with what in the SET and WHERE using below-ie val1-?1, val2-?4 etc?
The values in the SQL statement are filled in in the same order as they appear in the tuple.
So A=val1, B=val2, C=val3, X=val4, X=val5.
Reply
#5
Worked Perfect, problem resolved. Sorry for not using BBCode.
Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  An unexplainable error in .format statement - but only in a larger piece of code? ToniE 4 722 Sep-05-2023, 12:50 PM
Last Post: ToniE
Question Using SQLAlchemy, prevent SQLite3 table update by multiple program instances Calab 3 758 Aug-09-2023, 05:51 PM
Last Post: Calab
  String concatenation in SQL update statement hammer 3 1,539 Feb-24-2022, 08:00 PM
Last Post: hammer
  multiple condition if statement problem FelixReiter 3 2,611 Jan-11-2021, 08:07 AM
Last Post: FelixReiter
Question Python + Google Sheet | Best way to update specific cells in a single Update()? Vokofe 1 2,698 Dec-16-2020, 05:26 AM
Last Post: Vokofe
  Multiple conditions when indexing an array with or without np.where noob2305 1 2,681 Oct-25-2020, 02:06 PM
Last Post: jefsummers
  Looking for help in Parse multiple XMLs and update key node values and generate Out.. rajesh3383 0 1,882 Sep-15-2020, 01:42 PM
Last Post: rajesh3383
  Multiple conditions, one is null moralear27 1 2,204 Sep-13-2020, 06:11 AM
Last Post: scidam
  MYSQL Update Query format simdo01 1 2,237 Aug-31-2020, 12:59 AM
Last Post: nilamo
  multiple number format conversion oli_action 4 2,593 Aug-11-2020, 05:10 AM
Last Post: perfringo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020