0

I am trying to compile an SQL Update query from values in a PySimpleGUI fields. I am using concatenated f-strings to do this.

If any field is blank, one must substitute 'DEFAULT' as the value. To do that I tried to use a conditional_expressions, such as:

update_cust_qry = "UPDATE `d_base`.`Customers` SET " +\
f"Contact = '{'DEFAULT' if values['_CONTACT_'] = '' else values['_CONTACT_']}'," + \
f"WHERE Customer = '{values['_CUSTOMERS_'][0]}'"

Unfortunately, Python-3 declared such a construct as a syntax-error.

Can you please show me a correct way to do that?

Many, many thanks

Meir

MeirG
  • 333
  • 2
  • 14

1 Answers1

1

A minor error in your code: you have used = instead of == in the if condition. I tried the same construct with an inferred structure of the values container, and I get no syntax error, so the error is with the logic, not the f-strings.

values = {'_CONTACT_': "", '_CUSTOMERS_': [1,2,3]}
str = f"Contact = '{'DEFAULT' if values['_CONTACT_'] == '' else values['_CONTACT_']}'," + \
    f"WHERE Customer = '{values['_CUSTOMERS_'][0]}'"
print(str)

Output:

Contact = 'DEFAULT',WHERE Customer = '1' 

Now I try with: values = {'_CONTACT_': "test", '_CUSTOMERS_': [4]}

Output:

Contact = 'test',WHERE Customer = '4'
Shubham
  • 1,310
  • 4
  • 13