0

I'm trying to update a field based on a variable where in list...

urls = ["aaa", "bbb", "ccc", ...] (long list)
time_now = time.strftime('%Y-%m-%d %H:%M:%S')

UPDATE table SET updated_at=time_now WHERE name IN list

How would you do this in python?

This is my code so far:

update_time = time.strftime('%Y-%m-%d %H:%M:%S')
urls = ["http://asdf", "http://qwer"]
cur.execute('UPDATE sales SET updated_at=%s WHERE sale_url IN (%s)', (time_now, urls))
a_b
  • 1,828
  • 5
  • 23
  • 37
  • Use `%s` instead of `?` for the MySQL database adapter as it uses a different SQL parameter style, otherwise the principle is exactly the same. – Martijn Pieters Nov 22 '13 at 13:12
  • my problem is the syntax for setting updated_at to a variable at the same time as using the where in... – a_b Nov 22 '13 at 13:12
  • And you cannot use a sql parameter for that? `cursor.execute('UPDATE table SET updated_at=%s WHERE name in ({})'.format(...), [time_now] + long_list)`. – Martijn Pieters Nov 22 '13 at 13:13
  • Here `[time_now] + long_list` creates one list of parameter values for the SQL parameter placeholders. – Martijn Pieters Nov 22 '13 at 13:14

1 Answers1

0

try ;

"UPDATE table SET updated_at='%s' WHERE name IN ('%s')" % (time_now, "', '".join(list))
mccakici
  • 550
  • 3
  • 7