尝试添加到数据库,但忽略重复项

Attempting to add to database, but ignore duplicates

我正在尝试将 CSV 中的数据添加到 UNIQUE 字段中,但遇到一个问题,如果 CSV 中有任何重复项,程序将不会提交。我想要发生的是,如果您尝试添加重复项,只需继续其余部分,但在最后提交。我正在使用 SQLalchemy。就好像 db 遇到了一个错误,即使我有 try/except.

它也不会提交任何错误
import csv
import os

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker

engine = create_engine(os.getenv("DATABASE_URL"))
# scoped session allows us to have multiple users on a database
db = scoped_session(sessionmaker(bind=engine))

def main():
    # id = db.execute("SELECT id FROM authors WHERE name='Anne Rice'")
    # print(id['id'])
    with open("books.csv") as file:
        reader = csv.DictReader(file)
        for row in reader:
            # :placeholder, and then provide variables 
            try:
                db.execute("INSERT INTO authors1 (name) VALUES (:name)",
                            {"name":row['author']})
            except:
                pass
            print("added",row['author'])

        db.commit()

这是您的示例的简化版本,它可以工作,它将遍历作者列表并将它们添加到数据库中,当数据库由于 non-unique 值而给出 IntegrityError 时,它将打印出来并继续。

注意。我将相同的“打印添加”放在与您相同的级别,它将打印添加的作者和跳过的作者。

import sqlalchemy as sa

engine = sa.create_engine("sqlite:///:memory:", echo=True)

with engine.begin() as con:
    con.execute(
        "CREATE TABLE author ("
        "id INTEGER PRIMARY KEY, "
        "name TEXT NOT NULL UNIQUE)"
        )

authors = [
    "Guillaume Apollinaire",
    "Jules Verne",
    "Jean-Paul Sartre",
    "Jules Verne",
]

with engine.begin() as con:
    for author in authors:
        try:
            con.execute(
                "INSERT INTO author (name) VALUES (:name)",
                {"name": author}
            )
        except Exception as e:
            print(e)
        print(f"added {author}")

with engine.connect() as con:
    print(con.execute("SELECT * FROM author").all())

给出:

2022-06-04 11:53:26,812 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-06-04 11:53:26,813 INFO sqlalchemy.engine.Engine CREATE TABLE author (id INTEGER PRIMARY KEY, name TEXT NOT NULL UNIQUE)
2022-06-04 11:53:26,813 INFO sqlalchemy.engine.Engine [raw sql] ()
2022-06-04 11:53:26,816 INFO sqlalchemy.engine.Engine COMMIT
2022-06-04 11:53:29,950 INFO sqlalchemy.engine.Engine BEGIN (implicit)
2022-06-04 11:53:29,950 INFO sqlalchemy.engine.Engine INSERT INTO author (name) VALUES (:name)
2022-06-04 11:53:29,950 INFO sqlalchemy.engine.Engine [raw sql] {'name': 'Guillaume Apollinaire'}
added Guillaume Apollinaire
2022-06-04 11:53:29,950 INFO sqlalchemy.engine.Engine INSERT INTO author (name) VALUES (:name)
2022-06-04 11:53:29,950 INFO sqlalchemy.engine.Engine [raw sql] {'name': 'Jules Verne'}
added Jules Verne
2022-06-04 11:53:29,951 INFO sqlalchemy.engine.Engine INSERT INTO author (name) VALUES (:name)
2022-06-04 11:53:29,951 INFO sqlalchemy.engine.Engine [raw sql] {'name': 'Jean-Paul Sartre'}
added Jean-Paul Sartre
2022-06-04 11:53:29,951 INFO sqlalchemy.engine.Engine INSERT INTO author (name) VALUES (:name)
2022-06-04 11:53:29,951 INFO sqlalchemy.engine.Engine [raw sql] {'name': 'Jules Verne'}
(sqlite3.IntegrityError) UNIQUE constraint failed: author.name
[SQL: INSERT INTO author (name) VALUES (:name)]
[parameters: {'name': 'Jules Verne'}]
(Background on this error at: https://sqlalche.me/e/14/gkpj)
added Jules Verne
2022-06-04 11:53:29,960 INFO sqlalchemy.engine.Engine COMMIT
2022-06-04 11:59:34,511 INFO sqlalchemy.engine.Engine SELECT * FROM author
2022-06-04 11:59:34,511 INFO sqlalchemy.engine.Engine [raw sql] ()
[(1, 'Guillaume Apollinaire'), (2, 'Jules Verne'), (3, 'Jean-Paul Sartre')]