SQLite Query with Python 1
<p>I have generated a list and able to generate the result:</p>

<pre><code>l = [2, 3, 5, 2009 ]

cur1 = cur.execute('SELECT * from table where + or '.join(('Code = ' + str(n) for n in l)))
</code></pre>

<p>However, I want to put in another query in the same statement as follows:</p>

<pre><code>cur1 = cur.execute('SELECT * from table where '
**'Timestamp in (select max(timestamp) from table)'**
+ ' or '.join(('Code = ' + str(n) for n in l)))
</code></pre>

<p>but generated </p>

<pre><code> sqlite3.OperationalError: near "Code": syntax error. 
</code></pre>

<p>Please kindly advise how I can fit Timestamp statement into the first statement in order to focus on the query result which I want. </p>

<p>Your first was false and the second is of course false too.</p>

<pre><code>&gt;&gt;&gt; 'SELECT * from table where + or '.join(('Code = ' + str(n) for n in l))
&gt;&gt;&gt; 'Code = 2SELECT * from table where + or Code = 3SELECT * from table where + or Code = 5SELECT * from table where + or Code = 2009'
</code></pre>

<p>It's not a valid SQL statement.</p>

<p><a href="http://stackoverflow.com/questions/8159323/sqlite-select-equal-to-one-of-two-values">SQLite SELECT equal to one of two values</a></p>

<p>The right query statement should be:</p>

<pre><code>SELECT * FROM table WHERE Code in ('2', '3', '5', '2009')
</code></pre>

<p>You will get it with:</p>

<pre><code>cur.execute('SELECT * FROM table WHERE Code in ?', str(tuple(str(i) for i in l)))
</code></pre>

<p>Now you can use the same parameter for your second statement.</p>

<p><em>P/S: There is a better solution, but i don't have SQLite database to check it.</em> </p>

python sqlite python-3.x sqlite3