简体   繁体   中英

Python sqlite3: INSERT into table WHERE NOT EXISTS, using ? substitution parameter

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM