-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex3.py
47 lines (39 loc) · 1.13 KB
/
ex3.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Joins
from sqlalchemy import *
db = create_engine('sqlite:///joinDbDemo.db')
db.echo = True
metadata = MetaData(bind=db)
users = Table('users', metadata,
Column('user_id', Integer, primary_key=True),
Column('name', String(40)),
Column('age', Integer),
)
#users.create()
emails = Table('emails', metadata,
Column('email_id', Integer, primary_key=True),
Column('address', String),
Column('user_id', Integer, ForeignKey('users.user_id')),
)
#emails.create()
i = users.insert()
i.execute(
{'name': 'Mary', 'age': 30},
{'name': 'John', 'age': 42},
{'name': 'Susan', 'age': 57},
{'name': 'Carl', 'age': 33}
)
i = emails.insert()
i.execute(
# There's a better way to do this, but we haven't gotten there yet
{'address': '[email protected]', 'user_id': 1},
{'address': '[email protected]', 'user_id': 2},
{'address': '[email protected]', 'user_id': 2},
{'address': '[email protected]', 'user_id': 3},
{'address': '[email protected]', 'user_id': 4},
)
def run(stmt):
rs =stmt.execute()
for row in rs:
print row
s = select([users, emails], emails.c.user_id == users.c.user_id)
run(s)