Advertisement
Guest User

Untitled

a guest
Dec 4th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. baza danych python dodawanie osob
  2.  
  3.  
  4. from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
  5.  
  6. from sqlalchemy.orm import relationship, sessionmaker
  7. from sqlalchemy.ext.declarative import declarative_base
  8.  
  9.  
  10. engine = create_engine("sqlite:///Osoby.db")
  11.  
  12. Base = declarative_base()
  13. from sqlalchemy import Column, Integer, String
  14.  
  15. class Osoby(Base):
  16. __tablename__ = 'osoby'
  17. id = Column(Integer, primary_key=True)
  18. imie = Column(String(20), default='')
  19. nazwisko = Column(String(30), default='')
  20. adres_id = Column(String(30),ForeignKey('adresyzam.id'))
  21. adres = relationship('AdresyZam', backref='osoba')
  22. email = Column(String(20), default='')
  23. telefon = Column(Integer)
  24. emails = relationship('Email', backref='osoba')
  25.  
  26.  
  27. # def __repr__(self):
  28. # return "<Osoby(imie='%s', nazwisko='%s', email='%s', telefon='&i')>" % (self.imie, self.nazwisko, self.email, self.telefon)
  29.  
  30.  
  31. class Email(Base):
  32. __tablename__ = 'email'
  33. id = Column(Integer, primary_key=True)
  34. email = Column(String(100), default='')
  35. osoba_id = Column(Integer, ForeignKey('osoby.id'))
  36.  
  37.  
  38. class AdresyZam(Base):
  39. __tablename__ = 'adresyzam'
  40. id = Column(Integer, primary_key=True)
  41. ulica = Column(String(100), nullable=False)
  42. nr_domu = Column(Integer)
  43. nr_mieszkania = Column(Integer)
  44. kod = Column(Integer)
  45. miasta = Column(String(20), default='')
  46.  
  47.  
  48.  
  49.  
  50. #class Znajomi(Base):
  51. # __tablename__ = 'znajomi'
  52.  
  53.  
  54. # tworzymy tabele
  55. Base.metadata.create_all(engine)
  56.  
  57.  
  58. # tworzymy sesję, która przechowuje obiekty i umożliwia "rozmowę" z bazą
  59. BDSesja = sessionmaker(bind=engine)
  60. sesja = BDSesja()
  61.  
  62. # dodajemy dwie klasy, jeżeli tabela jest pusta
  63. if not sesja.query(Osoby).count():
  64. sesja.add(Osoby(imie ='Franciszek', nazwisko = 'Lolek', adres_id = 1,telefon = '1223'))
  65. sesja.add(Osoby(imie ='Roman', nazwisko = 'Bolek', adres_id = 3,telefon = '1213'))
  66.  
  67.  
  68. # tworzymy instancję klasy Osoby reprezentującą klasę "1A"
  69. inst_osoby = sesja.query(Osoby).filter_by(imie='Franciszek').one()
  70.  
  71. # Dodawanie danych do tabeli Email
  72. sesja.add_all([
  73. Email(id='1', email='franciszek1@gmail.com', osoba_id=inst_osoby.id),
  74. Email(id='2', email='franciszek2@gmail.com', osoba_id=inst_osoby.id),
  75. Email(id='3', email='franiu@gmail.com', osoba_id=inst_osoby.id),
  76. ])
  77.  
  78.  
  79.  
  80. def czytajdane():
  81. for email in sesja.query(Email).join(Osoby).all():
  82. print(email.id, email.email, email.osoba_id)
  83. print()
  84. for osoby in sesja.query(Osoby).join(Email).all():
  85. print(osoby.id, osoby.imie, osoby.nazwisko, osoby.adres_id)
  86.  
  87.  
  88. czytajdane()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement