Skip to content

Commit fddb7ef

Browse files
committed
Add SQLite3 notebook
1 parent e1326d2 commit fddb7ef

File tree

3 files changed

+349
-1
lines changed

3 files changed

+349
-1
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ Python notebooks written while learning some libs
1111

1212
[Plotly](https://plotly.com/python) - recreated the plots from matplotlib notebook using the doc.
1313

14-
[Requests](https://www.youtube.com/watch?v=tb8gHvYlCFs&ab_channel=CoreySchafer)
14+
[Requests](https://www.youtube.com/watch?v=tb8gHvYlCFs&ab_channel=CoreySchafer)
15+
16+
[SQLite3](https://www.youtube.com/watch?v=2WUo5tD-eIA&ab_channel=EduardoMendes)

SQLite3.ipynb

Lines changed: 346 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,346 @@
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": 2,
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+
"import sqlite3\n",
40+
"print(\"Python version:\", sys.version[:5])"
41+
]
42+
},
43+
{
44+
"source": [
45+
"# Creating a table in SQLite3"
46+
],
47+
"cell_type": "markdown",
48+
"metadata": {}
49+
},
50+
{
51+
"cell_type": "code",
52+
"execution_count": 5,
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"# Open connection\n",
57+
"con = sqlite3.connect('db/base.db')\n",
58+
"\n",
59+
"# Defining cursor\n",
60+
"cur = con.cursor()\n",
61+
"\n",
62+
"# Defining query\n",
63+
"sql = \"\"\"\n",
64+
"CREATE TABLE users (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n",
65+
" name TEXT NOT NULL,\n",
66+
" phone TEXT NOT NULL,\n",
67+
" email TEXT UNIQUE NOT NULL)\"\"\"\n",
68+
"# Attach sql to cursor \n",
69+
"cur.execute(sql)\n",
70+
"\n",
71+
"# Executes the sql in the db\n",
72+
"con.commit()\n",
73+
"\n",
74+
"# Close connection\n",
75+
"con.close()\n"
76+
]
77+
},
78+
{
79+
"source": [
80+
"# Insert"
81+
],
82+
"cell_type": "markdown",
83+
"metadata": {}
84+
},
85+
{
86+
"cell_type": "code",
87+
"execution_count": 8,
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"con = sqlite3.connect('db/base.db')\n",
92+
"\n",
93+
"cur = con.cursor()\n",
94+
"\n",
95+
"sql = \"\"\"\n",
96+
"INSERT INTO users (name, phone, email)\n",
97+
" VALUES(\"João Sousa\", \"21900000000\", \"joaovfsousa@live.com\")\n",
98+
"\"\"\"\n",
99+
"\n",
100+
"cur.execute(sql)\n",
101+
"\n",
102+
"con.commit()\n",
103+
"\n",
104+
"con.close()"
105+
]
106+
},
107+
{
108+
"source": [
109+
"# Defining some function to help"
110+
],
111+
"cell_type": "markdown",
112+
"metadata": {}
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 10,
117+
"metadata": {},
118+
"outputs": [],
119+
"source": [
120+
"def db_insert(name, phone, email):\n",
121+
" return f\"\"\"\n",
122+
" INSERT INTO users (name, phone, email)\n",
123+
" VALUES(\"{name}\", \"{phone}\", \"{email}\")\n",
124+
" \"\"\"\n",
125+
"\n",
126+
"con = sqlite3.connect('db/base.db')\n",
127+
"\n",
128+
"cur = con.cursor()\n",
129+
"\n",
130+
"cur.execute(db_insert(\"marivaldo\", \"999999998\", \"marivaldo@gmail.com\"))\n",
131+
"\n",
132+
"con.commit()\n",
133+
"\n",
134+
"con.close()"
135+
]
136+
},
137+
{
138+
"source": [
139+
"# Update"
140+
],
141+
"cell_type": "markdown",
142+
"metadata": {}
143+
},
144+
{
145+
"cell_type": "code",
146+
"execution_count": 15,
147+
"metadata": {},
148+
"outputs": [],
149+
"source": [
150+
"def db_update(name, email):\n",
151+
" return f\"\"\"\n",
152+
" UPDATE users SET name = \"{name}\" WHERE email = \"{email}\"\n",
153+
" \"\"\"\n",
154+
"\n",
155+
"con = sqlite3.connect('db/base.db')\n",
156+
"\n",
157+
"cur = con.cursor()\n",
158+
"\n",
159+
"cur.execute(db_update(\"Matheus\", \"joaovfsousa@live.com\"))\n",
160+
"\n",
161+
"con.commit()\n",
162+
"\n",
163+
"con.close()"
164+
]
165+
},
166+
{
167+
"source": [
168+
"# Delete"
169+
],
170+
"cell_type": "markdown",
171+
"metadata": {}
172+
},
173+
{
174+
"cell_type": "code",
175+
"execution_count": 17,
176+
"metadata": {},
177+
"outputs": [],
178+
"source": [
179+
"def db_delete(email):\n",
180+
" return f\"\"\"\n",
181+
" DELETE FROM users WHERE email=\"{email}\"\n",
182+
" \"\"\"\n",
183+
"\n",
184+
"con = sqlite3.connect('db/base.db')\n",
185+
"\n",
186+
"cur = con.cursor()\n",
187+
"\n",
188+
"cur.execute(db_delete(\"joaovfsousa@live.com\"))\n",
189+
"\n",
190+
"con.commit()\n",
191+
"\n",
192+
"con.close()"
193+
]
194+
},
195+
{
196+
"source": [
197+
"# Select"
198+
],
199+
"cell_type": "markdown",
200+
"metadata": {}
201+
},
202+
{
203+
"cell_type": "code",
204+
"execution_count": 19,
205+
"metadata": {},
206+
"outputs": [
207+
{
208+
"output_type": "stream",
209+
"name": "stdout",
210+
"text": [
211+
"(2, 'João', '21994677053', 'joaovfsousa@gmail.com')\n"
212+
]
213+
}
214+
],
215+
"source": [
216+
"def db_select(data, field):\n",
217+
" return f\"\"\"\n",
218+
" SELECT id, name, phone, email\n",
219+
" FROM users\n",
220+
" WHERE {field}={data}\n",
221+
" \"\"\"\n",
222+
"\n",
223+
"con = sqlite3.connect('db/base.db')\n",
224+
"\n",
225+
"cur = con.cursor()\n",
226+
"\n",
227+
"cur.execute(db_select(\"2\", \"id\"))\n",
228+
"\n",
229+
"#con.commit() not used once we want to return a value\n",
230+
"\n",
231+
"#returns one line from the result of the query\n",
232+
"data = cur.fetchone()\n",
233+
"\n",
234+
"con.close()\n",
235+
"\n",
236+
"print(data)\n"
237+
]
238+
},
239+
{
240+
"source": [
241+
"# Aplying decorators"
242+
],
243+
"cell_type": "markdown",
244+
"metadata": {}
245+
},
246+
{
247+
"cell_type": "code",
248+
"execution_count": 26,
249+
"metadata": {},
250+
"outputs": [],
251+
"source": [
252+
"def commit_close(func):\n",
253+
" def decorator(*args):\n",
254+
" con = sqlite3.connect('db/base.db')\n",
255+
" cur = con.cursor()\n",
256+
" sql = func(*args)\n",
257+
" cur.execute(sql)\n",
258+
" con.commit()\n",
259+
" con.close()\n",
260+
" return decorator"
261+
]
262+
},
263+
{
264+
"cell_type": "code",
265+
"execution_count": 29,
266+
"metadata": {},
267+
"outputs": [],
268+
"source": [
269+
"@commit_close\n",
270+
"def db_insert(name, phone, email):\n",
271+
" return f\"\"\"\n",
272+
" INSERT INTO users (name, phone, email)\n",
273+
" VALUES(\"{name}\", \"{phone}\", \"{email}\")\n",
274+
" \"\"\"\n",
275+
"@commit_close\n",
276+
"def db_update(name, email):\n",
277+
" return f\"\"\"\n",
278+
" UPDATE users SET name = \"{name}\" WHERE email = \"{email}\"\n",
279+
" \"\"\"\n",
280+
"@commit_close\n",
281+
"def db_delete(email):\n",
282+
" return f\"\"\"\n",
283+
" DELETE FROM users WHERE email=\"{email}\"\n",
284+
" \"\"\"\n",
285+
"\n",
286+
"def db_select(data, field):\n",
287+
" sql = f\"\"\"\n",
288+
" SELECT id, name, phone, email\n",
289+
" FROM users\n",
290+
" WHERE {field}={data}\n",
291+
" \"\"\"\n",
292+
"\n",
293+
" con = sqlite3.connect('db/base.db')\n",
294+
" cur = con.cursor()\n",
295+
" cur.execute(sql)\n",
296+
" data = cur.fetchall()\n",
297+
" con.close()\n",
298+
" return data\n"
299+
]
300+
},
301+
{
302+
"source": [
303+
"# Using the decorated functions"
304+
],
305+
"cell_type": "markdown",
306+
"metadata": {}
307+
},
308+
{
309+
"cell_type": "code",
310+
"execution_count": 28,
311+
"metadata": {},
312+
"outputs": [],
313+
"source": [
314+
"db_insert(\"Regis\", \"98989898\", \"regis_@gmail.com\")\n",
315+
"db_insert(\"Fabricio\", \"98989898\", \"fabricio_@gmail.com\")\n",
316+
"db_insert(\"Mazinho\", \"98989898\", \"mazinho_@gmail.com\")\n",
317+
"db_insert(\"Diego\", \"98989898\", \"diego_@gmail.com\")\n",
318+
"db_insert(\"Ricardo\", \"98989898\", \"ricardo_@gmail.com\")"
319+
]
320+
},
321+
{
322+
"cell_type": "code",
323+
"execution_count": 30,
324+
"metadata": {},
325+
"outputs": [
326+
{
327+
"output_type": "execute_result",
328+
"data": {
329+
"text/plain": [
330+
"[(5, 'Regis', '98989898', 'regis_@gmail.com'),\n",
331+
" (6, 'Fabricio', '98989898', 'fabricio_@gmail.com'),\n",
332+
" (7, 'Mazinho', '98989898', 'mazinho_@gmail.com'),\n",
333+
" (8, 'Diego', '98989898', 'diego_@gmail.com'),\n",
334+
" (9, 'Ricardo', '98989898', 'ricardo_@gmail.com')]"
335+
]
336+
},
337+
"metadata": {},
338+
"execution_count": 30
339+
}
340+
],
341+
"source": [
342+
"db_select(\"98989898\", \"phone\")"
343+
]
344+
}
345+
]
346+
}

db/base.db

16 KB
Binary file not shown.

0 commit comments

Comments
 (0)