Untitled

mail@pastecode.io avatar
unknown
python
2 years ago
722 B
2
Indexable
Never
from sqlalchemy import create_engine, Column, String, Integer
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import sessionmaker


database_url = 'mysql+mysqlconnector://root:haslomaslo123@localhost:3306/animals'
eng = create_engine(database_url)
base = declarative_base()

class Animals(base):
    __tablename__ = 'animals'
    id = Column(Integer, primary_key=True, autoincrement=True)
    name = Column(String(100))
    age = Column(Integer)


base.metadata.create_all(eng)
Session = sessionmaker(bind=eng)
session = Session()

animals = [
    {'name': 'Folklor', 'age': 7},
    {'name': 'Lars', 'age': 6}
]

# session.add_all(Animals(**animal) for animal in animals)
# session.commit()
session.close()