-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_expression.py
More file actions
63 lines (49 loc) · 2.06 KB
/
sql_expression.py
File metadata and controls
63 lines (49 loc) · 2.06 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from sqlalchemy import (
create_engine, Table, Column, Float, ForeignKey, Integer, String, MetaData
)
# executing the instructions from our localhost "chinook" db
db = create_engine("postgresql:///chinook")
meta = MetaData(db)
# create variable for "Artist" table
artist_table = Table(
"Artist", meta,
Column("ArtistId", Integer, primary_key=True),
Column("Name", String)
)
# create variable for "Album" table
album_table = Table(
"Album", meta,
Column("AlbumId", Integer, primary_key=True),
Column("Title", String),
Column("ArtistId", Integer, ForeignKey("artist_table.ArtistId"))
)
# create variable for "Track" table
track_table = Table(
"Track", meta,
Column("TrackId", Integer, primary_key=True),
Column("Name", String),
Column("AlbumId", Integer, ForeignKey("album_table.AlbumId")),
Column("MediaTypeId", Integer, primary_key=False),
Column("GenreId", Integer, primary_key=False),
Column("Composer", String),
Column("Milliseconds", Integer),
Column("Bytes", Integer),
Column("UnitPrice", Float)
)
# making the connection
with db.connect() as connection:
# Query 1 - select all records from the "Artist" table
# select_query = artist_table.select()
# Query 2 - select only the "Name" column from the "Artist" table
# select_query = artist_table.select().with_only_columns([artist_table.c.Name])
# Query 3 - select only "Queen" from the "Artist" table
# select_query = artist_table.select().where(artist_table.c.Name == "Queen")
# Query 4 - select only by "ArtistId" #51 from the "Artist" table
# select_query = artist_table.select().where(artist_table.c.ArtistId == 51)
# Query 5 - select only the albums with "ArtistId" #51 on the "Album" table
# select_query = album_table.select().where(album_table.c.ArtistId == 51)
# Query 6 - select all tracks where the composer is "Queen" from the "Track" table
select_query = track_table.select().where(track_table.c.Composer == "Queen")
results = connection.execute(select_query)
for result in results:
print(result)