How to define runPrepUpdate with variable for column name?

Hello,

I am trying to write a system.db.runPrepUpdate statement where I want to use a variable for the name of the column that gets updated as a result of this command.

For example, the following statement works and updates the StepNumber column correctly in MySQL table.

system.db.runPrepUpdate(""“UPDATE test.HVOverlay SET StepNumber=? WHERE Timestamp = ?”"",
[str(row[1]),str(row[0])],“MySQL”)

However, the column name could be different everytime. So, I need to use a variable instead of a fixed column name. I have been trying something like this
colname = str(columns[i])
system.db.runPrepUpdate(""“UPDATE test.HVOverlay SET %s=? WHERE Timestamp = ?”%(colname)"",
[str(row[1]),str(row[0])],“MySQL”)

But I am getting a syntax error. How can I define the system.db.runPrepUpdate statement when I am using a variable for the column name?

Thanks,

Shreyas

Hi Shreyas,

Try this:

colname = str(columns[i])
system.db.runPrepUpdate("""UPDATE test.HVOverlay SET %s=? WHERE Timestamp = ?"""% (colname), [str(row[1]),str(row[0])],"MySQL")
1 Like

Thank you Nick! It worked! Nice to hear from you!

Shreyas