Cleaned up some spacing/formatting. Modified main query to join in the State and Country tables.
This commit is contained in:
27
main.py
27
main.py
@@ -2,12 +2,10 @@ from sqlalchemy import create_engine, Column, Integer, String, BOOLEAN
|
||||
from sqlalchemy.ext.declarative import declarative_base
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
|
||||
|
||||
UserAddressID = []
|
||||
ActiveUser = []
|
||||
|
||||
|
||||
engine = create_engine('mssql+pymssql://IDV2Ridge:cWZSGWXS9muyYkHN*@10.10.10.50/IDV2Ridge',echo=False)
|
||||
engine = create_engine('mssql+pymssql://IDV2Ridge:cWZSGWXS9muyYkHN*@10.10.10.50/IDV2Ridge', echo=False)
|
||||
engine.connect()
|
||||
Base = declarative_base()
|
||||
|
||||
@@ -28,9 +26,9 @@ class Address(Base) :
|
||||
Line1 = Column(String)
|
||||
Line2 = Column(String)
|
||||
City = Column(String)
|
||||
State = Column(String)
|
||||
StateId = Column(Integer)
|
||||
PostalCode = Column(String)
|
||||
Country = Column(String)
|
||||
CountryId = Column(Integer)
|
||||
PhoneNumber = Column(String)
|
||||
EmailAddress = Column(String)
|
||||
|
||||
@@ -44,6 +42,16 @@ class User(Base):
|
||||
IsDeleted = Column(BOOLEAN)
|
||||
|
||||
|
||||
class State(Base):
|
||||
__tablename__ = 'State'
|
||||
Id = Column(Integer, primary_key=True)
|
||||
StateName = Column(String)
|
||||
|
||||
class Country(Base):
|
||||
__tablename__ = 'Country'
|
||||
Id = Column(Integer, primary_key=True)
|
||||
CountryName = Column(String)
|
||||
|
||||
Session = sessionmaker(bind=engine)
|
||||
session = Session()
|
||||
|
||||
@@ -57,10 +65,13 @@ for each in ActiveUser:
|
||||
UserName = each[1] + ' ' + each[2]
|
||||
print(UserName)
|
||||
# For each username we're filtering the UserAddress table by their userID and adding it to the UserAddressID array.
|
||||
# The UserAddressID array is then looped through and for each AddressID we pull the Address information
|
||||
# The UserAddressID array is then looped through and for each AddressID we pull the Address information,
|
||||
# State Information, and Country Information and combine it into one result
|
||||
|
||||
for ua in session.query(UserAddress).filter(UserAddress.UserId == each[0]):
|
||||
UserAddressID.append(ua.AddressId)
|
||||
for aid in UserAddressID:
|
||||
for cn in session.query(Address).filter(Address.Id==aid):
|
||||
print(cn.ContactName, cn.CompanyName, cn.Line1, cn.Line2)
|
||||
for cn in session.query(Address, State, Country).join(State, Address.StateId == State.Id).join(Country, Address.CountryId == Country.Id).filter(Address.Id == aid).all():
|
||||
print(cn[0].ContactName, cn[0].CompanyName, cn[0].Line1, cn[0].Line2, cn[0].City, cn[1].StateName, cn[0].PostalCode, cn[2].CountryName, cn[0].PhoneNumber, cn[0].EmailAddress)
|
||||
UserAddressID = []
|
||||
|
||||
|
||||
Reference in New Issue
Block a user