D
D
Dmitry Sergeev2011-09-28 13:59:59
MySQL
Dmitry Sergeev, 2011-09-28 13:59:59

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);

I get a warning Warning: Truncated incorrect DOUBLE value: '13364,13365' after which the query works only with the first number from the list.

Answer the question

In order to leave comments, you need to log in

8 answer(s)
K
Konstantin, 2011-09-28
@Norraxx

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!!!

N
nes, 2011-09-28
@nes

Try putting a space
ids = ', '.join(id);

D
Dmitry Sergeev, 2011-09-28
@JetMaster

same with space Warning: Truncated incorrect DOUBLE value: '13364, 13365'
just in case Python 2.6.5

D
Dmitry Sergeev, 2011-09-28
@JetMaster

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')

I don't know how to get rid of the quotes

A
alexmuz, 2011-09-28
@alexmuz

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);

D
Dmitry Sergeev, 2011-09-28
@JetMaster

ids = "','".join(vip_id);
cursor.execute("UPDATE articles SET sticky=0 WHERE id IN ('%s')", ids);

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13364\\',\\'13365'')" at line 1")
all the trouble is in quotation marks

D
Dmitry Sergeev, 2011-09-28
@JetMaster

ids = ','.join(vip_id);
cursor.execute('UPDATE articles SET sticky=0 WHERE id IN (%s)', ids);
print cursor._last_executed;

Warning: Truncated incorrect DOUBLE value: '13364,13365'

D
Dmitry Sergeev, 2011-09-28
@JetMaster

Digging on stackoverflow.com led to the syntax

ids = ', '.join(vip_id);
cursor.execute('UPDATE articles SET sticky=0 WHERE id IN (%s)' % (ids));

Works. I would like to get to the bottom of the truth of how the construction above works.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question