-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql_orm.py
More file actions
95 lines (71 loc) · 2.83 KB
/
sql_orm.py
File metadata and controls
95 lines (71 loc) · 2.83 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
from sqlalchemy import (
create_engine, Column, Float, ForeignKey, Integer, String
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
# executing the instructions from the "chinook" database
db = create_engine("postgresql:///chinook")
base = declarative_base()
# creat a class-based model fo the "Artist" table
class Artist(base):
__tablename__ = "Artist"
ArtistId = Column(Integer, primary_key=True)
Name = Column(String)
# create a class-based model for the "Album" table
class Album(base):
__tablename__ = "Album"
AlbumId = Column(Integer, primary_key=True)
Title = Column(String)
ArtistId = Column(Integer, ForeignKey("Artist.ArtistId"))
# create a class-based model for the "Track" table
class Track(base):
__tablename__ = "Track"
TrackId = Column(Integer, primary_key=True)
Name = Column(String)
AlbumId = Column(Integer, ForeignKey("Album.AlbumId"))
MediaTypeId = Column(Integer, primary_key=False)
GenreId = Column(Integer, primary_key=True)
Composer = Column(String)
Milliseconds = Column(Integer, primary_key=False)
Bytes = Column(Integer, primary_key=False)
UnitPrice = Column(Float)
# instead of connecting to the database directly, we will ask for a session
# create a new instance of sessionmaker, then point to our engine (the db)
Session = sessionmaker(db)
# opens an actual session by calling the Session() subclass defined above
session = Session()
# creating the database using declarative base subclass
base.metadata.create_all(db)
# Query 1 - select all records from the "Artist" table
# artists = session.query(Artist)
# for artist in artists:
# print(artist.ArtistId, artist.Name, sep=" | ")
# Query 2 - select only the "Name" column from the "Artist" table
# artists = session.query(Artist)
# for artist in artists:
# print(artist.Name)
# Query 3 - select only "Queen" from the "Artist" table
# artist = session.query(Artist).filter_by(Name="Queen").first()
# print(artist.ArtistId, artist.Name, sep=" | ")
# Query 4 - select only by "ArtistId" #51 from the "Artist" table
# artist = session.query(Artist).filter_by(ArtistId=51).first()
# print(artist.ArtistId, artist.Name, sep=" | ")
# Query 5 - select only the albums with "ArtistId" #51 on the "Album" table
# albums = session.query(Album).filter_by(ArtistId=51)
# for album in albums:
# print(album.AlbumId, album.Title, album.ArtistId, sep=" | ")
# Query 6 - select all tracks where the composer is "Queen" from the "Track" table
tracks = session.query(Track).filter_by(Composer="Queen")
for track in tracks:
print(
track.TrackId,
track.Name,
track.AlbumId,
track.MediaTypeId,
track.GenreId,
track.Composer,
track.Milliseconds,
track.Bytes,
track.UnitPrice,
sep=" | "
)