Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- #-*- coding: utf-8 -*-
- import sys
- from pip._vendor.distlib.compat import raw_input
- from sqlalchemy.ext.declarative import declarative_base
- from sqlalchemy import Column, Integer, String, ForeignKey
- from sqlalchemy.orm import relationship, relation, sessionmaker
- from sqlalchemy import create_engine
- Base = declarative_base()
- class Person(Base):
- __tablename__ = 'person'
- id = Column(Integer, primary_key=True)
- first_name = Column(String(255))
- last_name = Column(String(255))
- parent_id = Column(Integer, ForeignKey('person.id'))
- parent = relation('Person', remote_side=[id])
- class Email(Base):
- __tablename__ = 'email'
- id = Column(Integer, primary_key=True)
- email = Column(String(255))
- person_id = Column(Integer, ForeignKey('person.id'))
- person = relationship(Person)
- class Address(Base):
- __tablename__ = 'address'
- id = Column(Integer, primary_key=True)
- street = Column(String(255))
- city = Column(String(255))
- postal_code = Column(String(255))
- person_id = Column(Integer, ForeignKey('person.id'))
- person = relationship(Person)
- class Phone(Base):
- __tablename__ = 'phone'
- id = Column(Integer, primary_key=True)
- phone = Column(Integer)
- person_id = Column(Integer, ForeignKey('person.id'))
- person = relationship(Person)
- engine = create_engine('mysql+mysqlconnector://root:hasełko@localhost:3306/sqlalchemy')
- Base.metadata.bind = engine
- DBSession = sessionmaker(bind=engine)
- session = DBSession()
- def schema_create():
- Base.metadata.create_all(engine)
- print('Utworzono strukturę bazy danych')
- def add_person():
- while True:
- print('Dodawanie nowej osoby')
- first_name = raw_input('Imię: ')
- last_name = raw_input('Nazwisko: ')
- new_person = Person(first_name=first_name, last_name=last_name)
- session.add(new_person)
- session.commit()
- print('Zapisno! Teraz podaj adres')
- street = raw_input('Ulica: ')
- city = raw_input('Miasto: ')
- postal_code = raw_input('Kod pocztowy: ')
- new_address = Address(street=street, city=city, postal_code=postal_code, person=new_person)
- session.add(new_address)
- session.commit()
- print('Zapisno! Teraz podaj email')
- email = raw_input('Email: ')
- new_email = Email(email=email, person=new_person)
- session.add(new_email)
- session.commit()
- print('Zapisno! Teraz podaj numer telefonu')
- phone = raw_input('Numer telefonu: ')
- new_phone = Phone(phone=phone, person=new_person)
- session.add(new_phone)
- session.commit()
- print('Zapisno!')
- print('Wpisz (exit) aby zakończyć lub kliknij enter i dodaj nową osobę')
- cmd = raw_input('>>> ')
- if cmd.lower() == 'exit':
- break
- def seach_person():
- while True:
- print('Podaj imię lub jego część')
- imie = raw_input('Imię: ')
- imie = '%' + imie + '%'
- people = session.query(Person).filter(Person.first_name.like(imie)).all()
- if len(people) == 0:
- print('Brak wyników')
- else:
- for person in people:
- print(person.first_name, person.last_name)
- print('Wpisz (exit) aby zakończyć lub kliknij enter i szukaj dalej')
- cmd = raw_input('>>> ')
- if cmd.lower() == 'exit':
- break
- if __name__ == '__main__':
- if len(sys.argv) == 1:
- print('Brak argumentow')
- else:
- if sys.argv[1] == 'schema:create':
- schema_create()
- elif sys.argv[1] == 'add:person':
- add_person()
- elif sys.argv[1] == 'search:person':
- seach_person()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement