select name where id = "in the python list"?
<p>Let's say i have a python list of customer id like this:</p>

<pre><code>id = ('12','14','15','11',.......)
</code></pre>

<p>the array has 1000 values in it, and i need to insert the customer name to a table based on the ids from the list above.</p>

<p>my code is like:</p>

<pre><code>ids = ",".join(id)
sql = "insert into cust_table(name)values(names)where cust_id IN('ids')"
cursor.execute(sql)
</code></pre>

<p>after running the code, i get nothing inserted to the table. What mistake do i have?</p>

<p>Please help :(</p>

<p>Simply writing the name of a variable into a string doesn't magically make its contents appear in the string.</p>

<pre><code>&gt;&gt;&gt; p = 'some part'
&gt;&gt;&gt; s = 'replace p of a string'
&gt;&gt;&gt; s
'replace p of a string'
&gt;&gt;&gt; s = 'replace %s of a string' % p
&gt;&gt;&gt; s
'replace some part of a string'
&gt;&gt;&gt; s = 'replace {} of a string'.format(p)
&gt;&gt;&gt; s
'replace some part of a string'
</code></pre>

<p>In your case this would mean:</p>

<pre><code>&gt;&gt;&gt; sql = "insert into cust_table (name) values (names) where cust_id IN ('%s')"
&gt;&gt;&gt; ids = ", ".join(id)
&gt;&gt;&gt; cursor.execute(sql % ids)
</code></pre>

<p>although I strongly suspect that you have a similar problem with <code>names</code>.</p>

<p>In order to avoid possible sql injection problems, it would be preferable to use a "parameterized statement". This would look something like:</p>

<pre><code>&gt;&gt;&gt; sql = 'insert into ... where cust_id IN %s'
&gt;&gt;&gt; cursor.execute(sql, (id,))
</code></pre>

<p>Some database connectors for python are capable of this, but yours probably isn't.</p>

<p>A workaround might be something like</p>

<pre><code>&gt;&gt;&gt; params = ', '.join(['%s']*len(id))
&gt;&gt;&gt; sql = 'insert into ... where cust_id IN (%s)' % params
&gt;&gt;&gt; cursor.execute(sql, id)
</code></pre>

python mysql