Answer the question
In order to leave comments, you need to log in
Python&Mysql. How to connect the list correctly so that it can be used in a query?
id = ['13364', '13365'];
ids = ','.join(id);
cursor.execute("UPDATE articles SET sticky=0 WHERE id IN (%s)", ids);
Answer the question
In order to leave comments, you need to log in
In fact, you are perverts!
id = ['13364', '13365']
cursor.execute("UPDATE articles SET sticky=0 WHERE id IN (%s)" % (",".join(["%s"] * len(id)) , id)
I don't know the delimeter for the muscle, but don't put a semicolon in python!!!
same with space Warning: Truncated incorrect DOUBLE value: '13364, 13365'
just in case Python 2.6.5
I understood the reason. I found the _last_executed variable in the cursor which contains the last "compiled" query.
print cursor._last_executed;
UPDATE blurb_topics SET paid_service='', vip=0 WHERE id IN ('13364, 13365')
You have '13364', '13365' strings and it is logical that if you combine them with ',' you get the string '13364,13365'
ids = "','".join(id)
cursor.execute("UPDATE articles SET sticky=0 WHERE id IN ('%s')", ids);
ids = "','".join(vip_id);
cursor.execute("UPDATE articles SET sticky=0 WHERE id IN ('%s')", ids);
ids = ','.join(vip_id);
cursor.execute('UPDATE articles SET sticky=0 WHERE id IN (%s)', ids);
print cursor._last_executed;
Digging on stackoverflow.com led to the syntax
ids = ', '.join(vip_id);
cursor.execute('UPDATE articles SET sticky=0 WHERE id IN (%s)' % (ids));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question