Skip to content

Commit 801d6e9

Browse files
committed
small changes
1 parent 099577c commit 801d6e9

File tree

3 files changed

+185
-46
lines changed

3 files changed

+185
-46
lines changed

Notebooks/01_SQLite.ipynb

+153-35
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,16 @@
5050
"cur = conn.cursor()"
5151
]
5252
},
53+
{
54+
"cell_type": "markdown",
55+
"metadata": {},
56+
"source": [
57+
"#### Querying the sqlite_master table for table names"
58+
]
59+
},
5360
{
5461
"cell_type": "code",
55-
"execution_count": 5,
62+
"execution_count": 4,
5663
"metadata": {},
5764
"outputs": [
5865
{
@@ -70,7 +77,7 @@
7077
},
7178
{
7279
"cell_type": "code",
73-
"execution_count": 6,
80+
"execution_count": 5,
7481
"metadata": {},
7582
"outputs": [],
7683
"source": [
@@ -91,7 +98,7 @@
9198
},
9299
{
93100
"cell_type": "code",
94-
"execution_count": 8,
101+
"execution_count": 6,
95102
"metadata": {},
96103
"outputs": [],
97104
"source": [
@@ -101,7 +108,7 @@
101108
},
102109
{
103110
"cell_type": "code",
104-
"execution_count": 9,
111+
"execution_count": 7,
105112
"metadata": {},
106113
"outputs": [
107114
{
@@ -119,7 +126,7 @@
119126
},
120127
{
121128
"cell_type": "code",
122-
"execution_count": 10,
129+
"execution_count": 8,
123130
"metadata": {},
124131
"outputs": [
125132
{
@@ -138,16 +145,16 @@
138145
},
139146
{
140147
"cell_type": "code",
141-
"execution_count": 11,
148+
"execution_count": 9,
142149
"metadata": {},
143150
"outputs": [
144151
{
145152
"data": {
146153
"text/plain": [
147-
"<sqlite3.Cursor at 0x7fe55c78aa40>"
154+
"<sqlite3.Cursor at 0x7fe1fa654180>"
148155
]
149156
},
150-
"execution_count": 11,
157+
"execution_count": 9,
151158
"metadata": {},
152159
"output_type": "execute_result"
153160
}
@@ -163,7 +170,7 @@
163170
},
164171
{
165172
"cell_type": "code",
166-
"execution_count": 12,
173+
"execution_count": 10,
167174
"metadata": {},
168175
"outputs": [
169176
{
@@ -182,6 +189,16 @@
182189
" print(row)"
183190
]
184191
},
192+
{
193+
"cell_type": "code",
194+
"execution_count": 11,
195+
"metadata": {},
196+
"outputs": [],
197+
"source": [
198+
"conn.commit()\n",
199+
"conn.close()"
200+
]
201+
},
185202
{
186203
"cell_type": "markdown",
187204
"metadata": {},
@@ -191,15 +208,18 @@
191208
},
192209
{
193210
"cell_type": "code",
194-
"execution_count": 13,
211+
"execution_count": 12,
195212
"metadata": {},
196213
"outputs": [
197214
{
198215
"name": "stdout",
199216
"output_type": "stream",
200217
"text": [
201-
" date trans symbol qty price\n",
202-
"0 2006-01-05 BUY RHAT 100.0 35.14\n"
218+
" date trans symbol qty price\n",
219+
"0 2006-01-05 BUY RHAT 100.0 35.14\n",
220+
"1 2006-03-28 BUY IBM 1000.0 45.00\n",
221+
"2 2006-04-06 SELL IBM 500.0 53.00\n",
222+
"3 2006-04-05 BUY MSFT 1000.0 72.00\n"
203223
]
204224
}
205225
],
@@ -222,7 +242,7 @@
222242
},
223243
{
224244
"cell_type": "code",
225-
"execution_count": 14,
245+
"execution_count": 13,
226246
"metadata": {},
227247
"outputs": [],
228248
"source": [
@@ -235,22 +255,29 @@
235255
"source": [
236256
"### Accessing existing [Database](https://www.sqlitetutorial.net/sqlite-sample-database/):\n",
237257
"\n",
238-
"![Chinook Schema](../SampleDBs/sqlite-sample-database-color.jpg)"
258+
"![Chinook Schema](../Figs/sqlite-sample-database-color.jpg) \n",
259+
"[Source](https://www.sqlitetutorial.net/) "
239260
]
240261
},
241262
{
242263
"cell_type": "markdown",
243264
"metadata": {},
244265
"source": [
245-
"! wget https://cdn.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip\n",
246-
"! unzip chinook.zip\n",
247-
"! mv chinook.zip ../SampleDBs/chinook.sqlite\n",
248-
"! rm chinook.zip"
266+
"#### Retrieving original database"
267+
]
268+
},
269+
{
270+
"cell_type": "markdown",
271+
"metadata": {},
272+
"source": [
273+
"! wget https://cdn.sqlitetutorial.net/wp-content/uploads/2018/03/chinook.zip \n",
274+
"! unzip chinook.zip ../SampleDBs/chinook.sqlite \n",
275+
"! rm chinook.zip "
249276
]
250277
},
251278
{
252279
"cell_type": "code",
253-
"execution_count": 15,
280+
"execution_count": 14,
254281
"metadata": {},
255282
"outputs": [],
256283
"source": [
@@ -260,7 +287,7 @@
260287
},
261288
{
262289
"cell_type": "code",
263-
"execution_count": 17,
290+
"execution_count": 15,
264291
"metadata": {},
265292
"outputs": [
266293
{
@@ -293,9 +320,16 @@
293320
" print(c)"
294321
]
295322
},
323+
{
324+
"cell_type": "markdown",
325+
"metadata": {},
326+
"source": [
327+
"#### Querying the sqlite_master table to examine the table structure"
328+
]
329+
},
296330
{
297331
"cell_type": "code",
298-
"execution_count": 18,
332+
"execution_count": 17,
299333
"metadata": {},
300334
"outputs": [
301335
{
@@ -327,7 +361,7 @@
327361
},
328362
{
329363
"cell_type": "code",
330-
"execution_count": 19,
364+
"execution_count": 18,
331365
"metadata": {},
332366
"outputs": [
333367
{
@@ -361,7 +395,7 @@
361395
},
362396
{
363397
"cell_type": "code",
364-
"execution_count": 20,
398+
"execution_count": 19,
365399
"metadata": {},
366400
"outputs": [
367401
{
@@ -395,7 +429,7 @@
395429
},
396430
{
397431
"cell_type": "code",
398-
"execution_count": 22,
432+
"execution_count": 20,
399433
"metadata": {},
400434
"outputs": [
401435
{
@@ -463,7 +497,7 @@
463497
},
464498
{
465499
"cell_type": "code",
466-
"execution_count": 23,
500+
"execution_count": 22,
467501
"metadata": {},
468502
"outputs": [
469503
{
@@ -492,7 +526,7 @@
492526
},
493527
{
494528
"cell_type": "code",
495-
"execution_count": 24,
529+
"execution_count": 23,
496530
"metadata": {},
497531
"outputs": [
498532
{
@@ -527,7 +561,7 @@
527561
},
528562
{
529563
"cell_type": "code",
530-
"execution_count": 25,
564+
"execution_count": 24,
531565
"metadata": {},
532566
"outputs": [
533567
{
@@ -561,7 +595,7 @@
561595
},
562596
{
563597
"cell_type": "code",
564-
"execution_count": 26,
598+
"execution_count": 25,
565599
"metadata": {},
566600
"outputs": [
567601
{
@@ -594,7 +628,7 @@
594628
},
595629
{
596630
"cell_type": "code",
597-
"execution_count": 27,
631+
"execution_count": 26,
598632
"metadata": {},
599633
"outputs": [
600634
{
@@ -629,7 +663,7 @@
629663
},
630664
{
631665
"cell_type": "code",
632-
"execution_count": 28,
666+
"execution_count": 27,
633667
"metadata": {},
634668
"outputs": [
635669
{
@@ -664,7 +698,7 @@
664698
},
665699
{
666700
"cell_type": "code",
667-
"execution_count": 30,
701+
"execution_count": 28,
668702
"metadata": {},
669703
"outputs": [
670704
{
@@ -734,19 +768,103 @@
734768
" print(c)"
735769
]
736770
},
771+
{
772+
"cell_type": "markdown",
773+
"metadata": {},
774+
"source": [
775+
"#### Same with Pandas"
776+
]
777+
},
737778
{
738779
"cell_type": "code",
739780
"execution_count": 31,
740781
"metadata": {},
741-
"outputs": [],
782+
"outputs": [
783+
{
784+
"data": {
785+
"text/html": [
786+
"<div>\n",
787+
"<style scoped>\n",
788+
" .dataframe tbody tr th:only-of-type {\n",
789+
" vertical-align: middle;\n",
790+
" }\n",
791+
"\n",
792+
" .dataframe tbody tr th {\n",
793+
" vertical-align: top;\n",
794+
" }\n",
795+
"\n",
796+
" .dataframe thead th {\n",
797+
" text-align: right;\n",
798+
" }\n",
799+
"</style>\n",
800+
"<table border=\"1\" class=\"dataframe\">\n",
801+
" <thead>\n",
802+
" <tr style=\"text-align: right;\">\n",
803+
" <th></th>\n",
804+
" <th>Name</th>\n",
805+
" <th>Title</th>\n",
806+
" </tr>\n",
807+
" </thead>\n",
808+
" <tbody>\n",
809+
" <tr>\n",
810+
" <th>0</th>\n",
811+
" <td>A Cor Do Som</td>\n",
812+
" <td>None</td>\n",
813+
" </tr>\n",
814+
" <tr>\n",
815+
" <th>1</th>\n",
816+
" <td>AC/DC</td>\n",
817+
" <td>For Those About To Rock We Salute You</td>\n",
818+
" </tr>\n",
819+
" <tr>\n",
820+
" <th>2</th>\n",
821+
" <td>AC/DC</td>\n",
822+
" <td>Let There Be Rock</td>\n",
823+
" </tr>\n",
824+
" <tr>\n",
825+
" <th>3</th>\n",
826+
" <td>Aaron Copland &amp; London Symphony Orchestra</td>\n",
827+
" <td>A Copland Celebration, Vol. I</td>\n",
828+
" </tr>\n",
829+
" <tr>\n",
830+
" <th>4</th>\n",
831+
" <td>Aaron Goldberg</td>\n",
832+
" <td>Worlds</td>\n",
833+
" </tr>\n",
834+
" </tbody>\n",
835+
"</table>\n",
836+
"</div>"
837+
],
838+
"text/plain": [
839+
" Name \\\n",
840+
"0 A Cor Do Som \n",
841+
"1 AC/DC \n",
842+
"2 AC/DC \n",
843+
"3 Aaron Copland & London Symphony Orchestra \n",
844+
"4 Aaron Goldberg \n",
845+
"\n",
846+
" Title \n",
847+
"0 None \n",
848+
"1 For Those About To Rock We Salute You \n",
849+
"2 Let There Be Rock \n",
850+
"3 A Copland Celebration, Vol. I \n",
851+
"4 Worlds "
852+
]
853+
},
854+
"execution_count": 31,
855+
"metadata": {},
856+
"output_type": "execute_result"
857+
}
858+
],
742859
"source": [
743-
"#https://www.sqlitetutorial.net/"
860+
"df = pd.read_sql_query(query, conn)\n",
861+
"df.head()"
744862
]
745863
}
746864
],
747865
"metadata": {
748866
"kernelspec": {
749-
"display_name": "Python 3",
867+
"display_name": "Python 3 (ipykernel)",
750868
"language": "python",
751869
"name": "python3"
752870
},
@@ -760,7 +878,7 @@
760878
"name": "python",
761879
"nbconvert_exporter": "python",
762880
"pygments_lexer": "ipython3",
763-
"version": "3.7.6"
881+
"version": "3.9.5"
764882
}
765883
},
766884
"nbformat": 4,

0 commit comments

Comments
 (0)