0

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:

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

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

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

Thanks

1 Answer 1

2

To replace VALUES by SELECT should work

cursor.executemany('''
    INSERT INTO descriptions(desc)
    SELECT (?)
    WHERE NOT EXISTS (
        SELECT *
        FROM descriptions as d
        WHERE d.desc=?)''', 
    zip(descripts, descripts))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.