Advertisement
Guest User

Untitled

a guest
Jun 29th, 2015
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. #!/usr/bin/python
  2. #-*- coding: utf-8 -*-
  3.  
  4. import sys
  5. from pip._vendor.distlib.compat import raw_input
  6. from sqlalchemy.ext.declarative import declarative_base
  7. from sqlalchemy import Column, Integer, String, ForeignKey
  8. from sqlalchemy.orm import relationship, relation, sessionmaker
  9. from sqlalchemy import create_engine
  10.  
  11. Base = declarative_base()
  12.  
  13. class Person(Base):
  14. __tablename__ = 'person'
  15.  
  16. id = Column(Integer, primary_key=True)
  17. first_name = Column(String(255))
  18. last_name = Column(String(255))
  19. parent_id = Column(Integer, ForeignKey('person.id'))
  20. parent = relation('Person', remote_side=[id])
  21.  
  22. class Email(Base):
  23. __tablename__ = 'email'
  24.  
  25. id = Column(Integer, primary_key=True)
  26. email = Column(String(255))
  27. person_id = Column(Integer, ForeignKey('person.id'))
  28. person = relationship(Person)
  29.  
  30. class Address(Base):
  31. __tablename__ = 'address'
  32.  
  33. id = Column(Integer, primary_key=True)
  34. street = Column(String(255))
  35. city = Column(String(255))
  36. postal_code = Column(String(255))
  37. person_id = Column(Integer, ForeignKey('person.id'))
  38. person = relationship(Person)
  39.  
  40. class Phone(Base):
  41. __tablename__ = 'phone'
  42.  
  43. id = Column(Integer, primary_key=True)
  44. phone = Column(Integer)
  45. person_id = Column(Integer, ForeignKey('person.id'))
  46. person = relationship(Person)
  47.  
  48. engine = create_engine('mysql+mysqlconnector://root:hasełko@localhost:3306/sqlalchemy')
  49.  
  50. Base.metadata.bind = engine
  51. DBSession = sessionmaker(bind=engine)
  52. session = DBSession()
  53.  
  54. def schema_create():
  55. Base.metadata.create_all(engine)
  56. print('Utworzono strukturę bazy danych')
  57.  
  58. def add_person():
  59. while True:
  60. print('Dodawanie nowej osoby')
  61.  
  62. first_name = raw_input('Imię: ')
  63. last_name = raw_input('Nazwisko: ')
  64.  
  65. new_person = Person(first_name=first_name, last_name=last_name)
  66. session.add(new_person)
  67. session.commit()
  68. print('Zapisno! Teraz podaj adres')
  69.  
  70. street = raw_input('Ulica: ')
  71. city = raw_input('Miasto: ')
  72. postal_code = raw_input('Kod pocztowy: ')
  73.  
  74. new_address = Address(street=street, city=city, postal_code=postal_code, person=new_person)
  75. session.add(new_address)
  76. session.commit()
  77. print('Zapisno! Teraz podaj email')
  78.  
  79. email = raw_input('Email: ')
  80.  
  81. new_email = Email(email=email, person=new_person)
  82. session.add(new_email)
  83. session.commit()
  84. print('Zapisno! Teraz podaj numer telefonu')
  85.  
  86. phone = raw_input('Numer telefonu: ')
  87.  
  88. new_phone = Phone(phone=phone, person=new_person)
  89. session.add(new_phone)
  90. session.commit()
  91. print('Zapisno!')
  92.  
  93. print('Wpisz (exit) aby zakończyć lub kliknij enter i dodaj nową osobę')
  94. cmd = raw_input('>>> ')
  95. if cmd.lower() == 'exit':
  96. break
  97.  
  98. def seach_person():
  99. while True:
  100. print('Podaj imię lub jego część')
  101.  
  102. imie = raw_input('Imię: ')
  103. imie = '%' + imie + '%'
  104.  
  105. people = session.query(Person).filter(Person.first_name.like(imie)).all()
  106.  
  107. if len(people) == 0:
  108. print('Brak wyników')
  109. else:
  110. for person in people:
  111. print(person.first_name, person.last_name)
  112.  
  113. print('Wpisz (exit) aby zakończyć lub kliknij enter i szukaj dalej')
  114. cmd = raw_input('>>> ')
  115. if cmd.lower() == 'exit':
  116. break
  117.  
  118. if __name__ == '__main__':
  119. if len(sys.argv) == 1:
  120. print('Brak argumentow')
  121. else:
  122. if sys.argv[1] == 'schema:create':
  123. schema_create()
  124. elif sys.argv[1] == 'add:person':
  125. add_person()
  126. elif sys.argv[1] == 'search:person':
  127. seach_person()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement