Python sqlite3: INSERT into table WHERE NOT EXISTS, using ? substitution parameter
<p>I'm creating a table of descriptions from a list of not necessarily unique descriptions. I would like the table to contain only distinct descriptions, so while inserting descriptions into the table, I need to check to see if they already exist. My code(simplified) looks something like as follows:</p>

<pre><code>cur.execute(''' CREATE TABLE descriptions
            (id INTEGER PRIMARY KEY AUTOINCREMENT, desc TEXT)''')

descripts  = ["d1", "d2", "d3", "d4", "d3", "d1", "d5", "d6", "d7", "d2"]
cur.executemany('''
    INSERT INTO descriptions(desc) 
    VALUES (?) 
    WHERE NOT EXISTS (
        SELECT * 
        FROM descriptions as d 
        WHERE d.desc=?) 
        ''', zip(descripts, descripts))
</code></pre>

<p>The result is <code>OperationalError: near "WHERE": syntax error</code>, and I'm not sure exactly where I'm going wrong.</p>

<p>Just a note: I realize I could solve this using a <code>set()</code> structure in python, but for academic reasons this is not permitted.</p>

<p>Thanks</p>

<p>To replace <code>VALUES</code> by <code>SELECT</code> should work</p>

<pre><code>cursor.executemany('''
    INSERT INTO descriptions(desc)
    SELECT (?)
    WHERE NOT EXISTS (
        SELECT *
        FROM descriptions as d
        WHERE d.desc=?)''', 
    zip(descripts, descripts))
</code></pre>

python python-2.7 sqlite sqlite3