Skip to content

Commit e9734cb

Browse files
committed
Add SQLAlchemyORM notebook
1 parent 4bb5559 commit e9734cb

File tree

4 files changed

+271
-8
lines changed

4 files changed

+271
-8
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# LearningPythonLibs
2+
23
Python notebooks written while learning some libs
34

45
[Numpy](https://www.youtube.com/watch?v=GB9ByFAIAH4&list=PLFCB5Dp81iNVmuoGIqcT5oF4K-7kTI5vp&index=7&ab_channel=KeithGalli)
@@ -19,4 +20,6 @@ Python notebooks written while learning some libs
1920

2021
[SQLAchemyCore](https://www.youtube.com/watch?v=rBIksyGY4_E&t=2146s&ab_channel=EduardoMendes)
2122

22-
[Shutil, os, os.path](https://www.youtube.com/watch?v=8cNpLnH1dsU&ab_channel=EduardoMendes)
23+
[Shutil, os, os.path](https://www.youtube.com/watch?v=8cNpLnH1dsU&ab_channel=EduardoMendes)
24+
25+
[SQLAchemyORM](https://www.youtube.com/watch?v=UgaybOYMKS0&ab_channel=EduardoMendes)

SQLAlchemyCore.ipynb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,13 +315,6 @@
315315
"\n",
316316
"result.rowcount"
317317
]
318-
},
319-
{
320-
"cell_type": "code",
321-
"execution_count": null,
322-
"metadata": {},
323-
"outputs": [],
324-
"source": []
325318
}
326319
]
327320
}

SQLAlchemyORM.ipynb

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
{
2+
"metadata": {
3+
"language_info": {
4+
"codemirror_mode": {
5+
"name": "ipython",
6+
"version": 3
7+
},
8+
"file_extension": ".py",
9+
"mimetype": "text/x-python",
10+
"name": "python",
11+
"nbconvert_exporter": "python",
12+
"pygments_lexer": "ipython3",
13+
"version": "3.8.6-final"
14+
},
15+
"orig_nbformat": 2,
16+
"kernelspec": {
17+
"name": "python3",
18+
"display_name": "Python 3"
19+
}
20+
},
21+
"nbformat": 4,
22+
"nbformat_minor": 2,
23+
"cells": [
24+
{
25+
"cell_type": "code",
26+
"execution_count": 1,
27+
"metadata": {},
28+
"outputs": [
29+
{
30+
"output_type": "stream",
31+
"name": "stdout",
32+
"text": [
33+
"Python version: 3.8.6\n"
34+
]
35+
}
36+
],
37+
"source": [
38+
"import sys\n",
39+
"print(\"Python version:\", sys.version[:5])\n",
40+
"from pprint import pprint\n",
41+
"\n",
42+
"from sqlalchemy.ext.declarative import declarative_base\n",
43+
"from sqlalchemy import create_engine, Column, Integer, String, ForeignKey\n",
44+
"from sqlalchemy.orm import sessionmaker, relationship"
45+
]
46+
},
47+
{
48+
"source": [
49+
"# Initializing engine and session"
50+
],
51+
"cell_type": "markdown",
52+
"metadata": {}
53+
},
54+
{
55+
"cell_type": "code",
56+
"execution_count": 2,
57+
"metadata": {},
58+
"outputs": [],
59+
"source": [
60+
"engine = create_engine(\"sqlite:///db/sqlachemyorm.db\",echo=True)\n",
61+
"\n",
62+
"Session = sessionmaker(bind=engine)\n",
63+
"session = Session()"
64+
]
65+
},
66+
{
67+
"source": [
68+
"# Creating table"
69+
],
70+
"cell_type": "markdown",
71+
"metadata": {}
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": 3,
76+
"metadata": {},
77+
"outputs": [
78+
{
79+
"output_type": "stream",
80+
"name": "stdout",
81+
"text": [
82+
"2020-11-05 17:27:47,954 INFO sqlalchemy.engine.base.Engine SELECT CAST('test plain returns' AS VARCHAR(60)) AS anon_1\n",
83+
"2020-11-05 17:27:47,955 INFO sqlalchemy.engine.base.Engine ()\n",
84+
"2020-11-05 17:27:47,956 INFO sqlalchemy.engine.base.Engine SELECT CAST('test unicode returns' AS VARCHAR(60)) AS anon_1\n",
85+
"2020-11-05 17:27:47,957 INFO sqlalchemy.engine.base.Engine ()\n",
86+
"2020-11-05 17:27:47,958 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info(\"products\")\n",
87+
"2020-11-05 17:27:47,959 INFO sqlalchemy.engine.base.Engine ()\n",
88+
"2020-11-05 17:27:47,960 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info(\"products\")\n",
89+
"2020-11-05 17:27:47,961 INFO sqlalchemy.engine.base.Engine ()\n",
90+
"2020-11-05 17:27:47,962 INFO sqlalchemy.engine.base.Engine PRAGMA main.table_info(\"people\")\n",
91+
"2020-11-05 17:27:47,963 INFO sqlalchemy.engine.base.Engine ()\n",
92+
"2020-11-05 17:27:47,963 INFO sqlalchemy.engine.base.Engine PRAGMA temp.table_info(\"people\")\n",
93+
"2020-11-05 17:27:47,964 INFO sqlalchemy.engine.base.Engine ()\n",
94+
"2020-11-05 17:27:47,965 INFO sqlalchemy.engine.base.Engine \n",
95+
"CREATE TABLE people (\n",
96+
"\tid INTEGER NOT NULL, \n",
97+
"\tname VARCHAR, \n",
98+
"\temail VARCHAR, \n",
99+
"\tage INTEGER, \n",
100+
"\tPRIMARY KEY (id)\n",
101+
")\n",
102+
"\n",
103+
"\n",
104+
"2020-11-05 17:27:47,966 INFO sqlalchemy.engine.base.Engine ()\n",
105+
"2020-11-05 17:27:47,971 INFO sqlalchemy.engine.base.Engine COMMIT\n",
106+
"2020-11-05 17:27:47,972 INFO sqlalchemy.engine.base.Engine \n",
107+
"CREATE TABLE products (\n",
108+
"\tid INTEGER NOT NULL, \n",
109+
"\tname VARCHAR, \n",
110+
"\tperson_id INTEGER, \n",
111+
"\tPRIMARY KEY (id), \n",
112+
"\tFOREIGN KEY(person_id) REFERENCES people (id)\n",
113+
")\n",
114+
"\n",
115+
"\n",
116+
"2020-11-05 17:27:47,973 INFO sqlalchemy.engine.base.Engine ()\n",
117+
"2020-11-05 17:27:47,976 INFO sqlalchemy.engine.base.Engine COMMIT\n"
118+
]
119+
}
120+
],
121+
"source": [
122+
"Base = declarative_base()\n",
123+
"\n",
124+
"# Relation\n",
125+
"class Product(Base):\n",
126+
" __tablename__ = 'products'\n",
127+
"\n",
128+
" id = Column(Integer, primary_key=True)\n",
129+
" name = Column(String)\n",
130+
" person_id = Column(Integer, ForeignKey(\"people.id\"))\n",
131+
" person = relationship('Person')\n",
132+
"\n",
133+
" def __repr__(self):\n",
134+
" return f\"Product(name={self.name}, person={self.person})\"\n",
135+
"\n",
136+
"class Person(Base):\n",
137+
" __tablename__ = \"people\"\n",
138+
" \n",
139+
" id = Column(Integer, primary_key=True)\n",
140+
" name = Column(String)\n",
141+
" email = Column(String)\n",
142+
" age = Column(Integer)\n",
143+
" products = relationship(Product, backref='people')\n",
144+
"\n",
145+
" def __repr__(self):\n",
146+
" return f'Person(id={self.id}, name={self.name}, age={self.age}, email={self.email}, products={self.products})'\n",
147+
"\n",
148+
"\n",
149+
"Base.metadata.create_all(engine)"
150+
]
151+
},
152+
{
153+
"source": [
154+
"# Inserting data"
155+
],
156+
"cell_type": "markdown",
157+
"metadata": {}
158+
},
159+
{
160+
"cell_type": "code",
161+
"execution_count": 4,
162+
"metadata": {},
163+
"outputs": [
164+
{
165+
"output_type": "stream",
166+
"name": "stdout",
167+
"text": [
168+
"2020-11-05 17:27:48,006 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
169+
"2020-11-05 17:27:48,008 INFO sqlalchemy.engine.base.Engine INSERT INTO people (name, email, age) VALUES (?, ?, ?)\n",
170+
"2020-11-05 17:27:48,009 INFO sqlalchemy.engine.base.Engine ('Fausto', 'fausto@gmail.com', 10)\n",
171+
"2020-11-05 17:27:48,011 INFO sqlalchemy.engine.base.Engine INSERT INTO people (name, email, age) VALUES (?, ?, ?)\n",
172+
"2020-11-05 17:27:48,012 INFO sqlalchemy.engine.base.Engine ('João', 'joaovfsousa@gmail.com', 19)\n",
173+
"2020-11-05 17:27:48,013 INFO sqlalchemy.engine.base.Engine INSERT INTO people (name, email, age) VALUES (?, ?, ?)\n",
174+
"2020-11-05 17:27:48,014 INFO sqlalchemy.engine.base.Engine ('Fernando', 'fernando@gmail.com', 23)\n",
175+
"2020-11-05 17:27:48,014 INFO sqlalchemy.engine.base.Engine INSERT INTO people (name, email, age) VALUES (?, ?, ?)\n",
176+
"2020-11-05 17:27:48,016 INFO sqlalchemy.engine.base.Engine ('Pedro', 'pedro@gmail.com', 48)\n",
177+
"2020-11-05 17:27:48,017 INFO sqlalchemy.engine.base.Engine COMMIT\n"
178+
]
179+
}
180+
],
181+
"source": [
182+
"p1 = Person(name=\"Fausto\", email=\"fausto@gmail.com\", age=10)\n",
183+
"p2 = Person(name=\"João\", email=\"joaovfsousa@gmail.com\", age=19)\n",
184+
"p3 = Person(name=\"Fernando\", email=\"fernando@gmail.com\", age=23)\n",
185+
"p4 = Person(name=\"Pedro\", email=\"pedro@gmail.com\", age=48)\n",
186+
"\n",
187+
"session.add(p1)\n",
188+
"session.add_all([p2, p3, p4])\n",
189+
"\n",
190+
"session.commit()"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": 5,
196+
"metadata": {},
197+
"outputs": [
198+
{
199+
"output_type": "stream",
200+
"name": "stdout",
201+
"text": [
202+
"2020-11-05 17:27:48,050 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)\n",
203+
"2020-11-05 17:27:48,051 INFO sqlalchemy.engine.base.Engine INSERT INTO people (name, email, age) VALUES (?, ?, ?)\n",
204+
"2020-11-05 17:27:48,051 INFO sqlalchemy.engine.base.Engine ('Leonardo', 'leo@gmail.com', 41)\n"
205+
]
206+
}
207+
],
208+
"source": [
209+
"p5 = Person(name=\"Leonardo\", email=\"leo@gmail.com\", age=41)\n",
210+
"\n",
211+
"session.add(p5)\n",
212+
"session.flush()"
213+
]
214+
},
215+
{
216+
"source": [
217+
"# Select"
218+
],
219+
"cell_type": "markdown",
220+
"metadata": {}
221+
},
222+
{
223+
"cell_type": "code",
224+
"execution_count": 6,
225+
"metadata": {},
226+
"outputs": [
227+
{
228+
"output_type": "stream",
229+
"name": "stdout",
230+
"text": [
231+
"2020-11-05 17:27:48,078 INFO sqlalchemy.engine.base.Engine SELECT people.id AS people_id, people.name AS people_name, people.email AS people_email, people.age AS people_age \n",
232+
"FROM people\n",
233+
"2020-11-05 17:27:48,079 INFO sqlalchemy.engine.base.Engine ()\n",
234+
"2020-11-05 17:27:48,081 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.person_id AS products_person_id \n",
235+
"FROM products \n",
236+
"WHERE ? = products.person_id\n",
237+
"2020-11-05 17:27:48,082 INFO sqlalchemy.engine.base.Engine (1,)\n",
238+
"2020-11-05 17:27:48,083 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.person_id AS products_person_id \n",
239+
"FROM products \n",
240+
"WHERE ? = products.person_id\n",
241+
"2020-11-05 17:27:48,083 INFO sqlalchemy.engine.base.Engine (2,)\n",
242+
"2020-11-05 17:27:48,084 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.person_id AS products_person_id \n",
243+
"FROM products \n",
244+
"WHERE ? = products.person_id\n",
245+
"2020-11-05 17:27:48,085 INFO sqlalchemy.engine.base.Engine (3,)\n",
246+
"2020-11-05 17:27:48,086 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.person_id AS products_person_id \n",
247+
"FROM products \n",
248+
"WHERE ? = products.person_id\n",
249+
"2020-11-05 17:27:48,087 INFO sqlalchemy.engine.base.Engine (4,)\n",
250+
"2020-11-05 17:27:48,088 INFO sqlalchemy.engine.base.Engine SELECT products.id AS products_id, products.name AS products_name, products.person_id AS products_person_id \n",
251+
"FROM products \n",
252+
"WHERE ? = products.person_id\n",
253+
"2020-11-05 17:27:48,089 INFO sqlalchemy.engine.base.Engine (5,)\n",
254+
"[Person(id=1, name=Fausto, age=10, email=fausto@gmail.com, products=[]),\n",
255+
" Person(id=2, name=João, age=19, email=joaovfsousa@gmail.com, products=[]),\n",
256+
" Person(id=3, name=Fernando, age=23, email=fernando@gmail.com, products=[]),\n",
257+
" Person(id=4, name=Pedro, age=48, email=pedro@gmail.com, products=[]),\n",
258+
" Person(id=5, name=Leonardo, age=41, email=leo@gmail.com, products=[])]\n"
259+
]
260+
}
261+
],
262+
"source": [
263+
"pprint(session.query(Person).all())"
264+
]
265+
}
266+
]
267+
}

db/sqlachemyorm.db

12 KB
Binary file not shown.

0 commit comments

Comments
 (0)