weppage скранивается, результаты временно сохраняются в соответствии с `project_min_example` (Строительство в кодовом блоке ниже).
после сеанса скрапения, я хочу навсегда хранить, чтобы с использованием, с помощью. Чтобы избежать наличия одних и тех же данных в моей базе данных из -за поиска одних и тех же данных во время нескольких сеансов скребки, идентификатор столбца настроен только для принятия уникальных значений: < /p>
Код: Выделить всё
ID = Column(Integer, nullable=False, unique=True)
Код: Выделить всё
from sqlalchemy import create_engine
from sqlalchemy.orm import declarative_base
from sqlalchemy import Column
from sqlalchemy.sql.sqltypes import Integer, String
from sqlalchemy.orm import sessionmaker
import sqlalchemy.exc
base = declarative_base()
class Project(base):
__tablename__ = 'projects' # Tabellenblatt
index = Column(Integer, primary_key=True, autoincrement=True)
ID = Column(Integer, nullable=False, unique=True)
title = Column(String(200), nullable=False)
def __init__(self, project: dict) -> None:
self.__dict__.update(project)
if __name__ == '__main__':
project_min_example = {
'metadata': {'info_1': 'abc'},
'projects_data': [
{'ID': '1',
'title': 'project_1'},
{'ID': '2',
'title': 'project_2'}
]
}
engine = create_engine("sqlite:///projects_minExample.db")
conn = engine.connect()
Session = sessionmaker()
base.metadata.create_all(conn)
for project in project_min_example['projects_data']:
print('*********************************************')
print(f"Project {project['title']}, (ID {project['ID']}):")
try:
with Session(bind=engine) as session:
project_SQLA = Project(project)
session.add(project_SQLA)
session.commit()
print('successfully added to db')
except sqlalchemy.exc.IntegrityError as e:
print(e)
e.add_note('project already existing in database')
print('project already existing')
< /code>
Прямо сейчас я хочу обработать только случай исключения, как описано выше, который дает это сообщение об ошибке:
(sqlite3.IntegrityError) UNIQUE constraint failed: projects.ID< /code>
[SQL: INSERT INTO projects ("ID", title) VALUES (?, ?)]< /code>
[parameters: ('1', 'project_1')]< /code>
(Background on this error at: https://sqlalche.me/e/20/gkpj)< /code>
(I had to format the error message as code in order to enable posting my question)
Other possible integrity error types shall really produce errors, or be handled in a more general way (not implemented in the code example).
I found out, that `sqlalchemy.exc.IntegrityError` accepts arguments and I believe they could be used to pass error details. Unfortunately the documentation I found did not help me to understand how to correctly pass them.
My questions:
- Can I use the `sqlalchemy.exc.IntegrityError` arguments to provide error details ?
- How to properly pass the arguments ?
I tried:
# same imports as above
import sqlite3
# same code as above
for project in project_min_example['projects_data']:
print('*********************************************')
print(f"Project {project['title']}, (ID {project['ID']}):")
try:
with Session(bind=engine) as session:
project_SQLA = Project(project)
session.add(project_SQLA)
session.commit()
print('successfully added to db')
# except sqlalchemy.exc.IntegrityError as e:
except sqlalchemy.exc.IntegrityError(statement='(sqlite3.IntegrityError) UNIQUE constraint failed: projects.Projekt_ID', params=project['ID'], orig=sqlite3.IntegrityError) as e:
print(e)
e.add_note('project already existing in database')
print('project already existing')
Подробнее здесь: https://stackoverflow.com/questions/775 ... on-details