Skip to content

Commit 44b2d24

Browse files
committed
added example of SQLite
1 parent 75346c6 commit 44b2d24

File tree

3 files changed

+132
-21
lines changed

3 files changed

+132
-21
lines changed

Figs/Nursery.png

100644100755
File mode changed.

Notebooks/01_SQLite.ipynb

+132-21
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@
7575
"print(cur.fetchall())"
7676
]
7777
},
78+
{
79+
"cell_type": "markdown",
80+
"metadata": {},
81+
"source": [
82+
"### Creating a Table and inserzing Data"
83+
]
84+
},
7885
{
7986
"cell_type": "code",
8087
"execution_count": 5,
@@ -151,7 +158,7 @@
151158
{
152159
"data": {
153160
"text/plain": [
154-
"<sqlite3.Cursor at 0x7fe1fa654180>"
161+
"<sqlite3.Cursor at 0x7f12fb4210a0>"
155162
]
156163
},
157164
"execution_count": 9,
@@ -203,7 +210,7 @@
203210
"cell_type": "markdown",
204211
"metadata": {},
205212
"source": [
206-
"#### Using Pandas"
213+
"#### Using Pandas to query"
207214
]
208215
},
209216
{
@@ -249,6 +256,110 @@
249256
"os.remove(os.path.join(\"..\",\"SampleDBs\",'sqlite_example.db'))"
250257
]
251258
},
259+
{
260+
"cell_type": "markdown",
261+
"metadata": {},
262+
"source": [
263+
"### Another example"
264+
]
265+
},
266+
{
267+
"cell_type": "code",
268+
"execution_count": 14,
269+
"metadata": {},
270+
"outputs": [],
271+
"source": [
272+
"conn = sqlite3.connect(os.path.join(\"..\",\"SampleDBs\",\"test.db\"))\n",
273+
" \n",
274+
"#Here, you can also supply database name as the special name :memory: to create a database in RAM."
275+
]
276+
},
277+
{
278+
"cell_type": "code",
279+
"execution_count": 15,
280+
"metadata": {},
281+
"outputs": [],
282+
"source": [
283+
"conn.execute('''CREATE TABLE COMPANY \n",
284+
" (ID INT PRIMARY KEY NOT NULL,\n",
285+
" NAME TEXT NOT NULL,\n",
286+
" AGE INT NOT NULL,\n",
287+
" ADDRESS CHAR(50),\n",
288+
" SALARY REAL);''')\n",
289+
"conn.close()"
290+
]
291+
},
292+
{
293+
"cell_type": "code",
294+
"execution_count": 16,
295+
"metadata": {},
296+
"outputs": [],
297+
"source": [
298+
"conn = sqlite3.connect(os.path.join(\"..\",\"SampleDBs\",\"test.db\"))\n",
299+
"\n",
300+
"conn.execute(\"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (1, 'Paul', 32, 'California', 20000.00 )\");\n",
301+
"conn.execute(\"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (2, 'Allen', 25, 'Texas', 15000.00 )\");\n",
302+
"conn.execute(\"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (3, 'Teddy', 23, 'Norway', 20000.00 )\");\n",
303+
"conn.execute(\"INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES (4, 'Mark', 25, 'Rich-Mond ', 65000.00 )\");\n",
304+
"\n",
305+
"conn.commit()\n",
306+
"conn.close()"
307+
]
308+
},
309+
{
310+
"cell_type": "code",
311+
"execution_count": 17,
312+
"metadata": {},
313+
"outputs": [
314+
{
315+
"name": "stdout",
316+
"output_type": "stream",
317+
"text": [
318+
"ID = 1\n",
319+
"NAME = Paul\n",
320+
"ADDRESS = California\n",
321+
"SALARY = 20000.0 \n",
322+
"\n",
323+
"ID = 2\n",
324+
"NAME = Allen\n",
325+
"ADDRESS = Texas\n",
326+
"SALARY = 15000.0 \n",
327+
"\n",
328+
"ID = 3\n",
329+
"NAME = Teddy\n",
330+
"ADDRESS = Norway\n",
331+
"SALARY = 20000.0 \n",
332+
"\n",
333+
"ID = 4\n",
334+
"NAME = Mark\n",
335+
"ADDRESS = Rich-Mond \n",
336+
"SALARY = 65000.0 \n",
337+
"\n"
338+
]
339+
}
340+
],
341+
"source": [
342+
"conn = sqlite3.connect(os.path.join(\"..\",\"SampleDBs\",\"test.db\"))\n",
343+
"\n",
344+
"cursor = conn.execute(\"SELECT id, name, address, salary from COMPANY\")\n",
345+
"for row in cursor:\n",
346+
" print(\"ID = \", row[0])\n",
347+
" print(\"NAME = \", row[1])\n",
348+
" print(\"ADDRESS = \", row[2])\n",
349+
" print(\"SALARY = \", row[3], \"\\n\")\n",
350+
" \n",
351+
"conn.close()"
352+
]
353+
},
354+
{
355+
"cell_type": "code",
356+
"execution_count": 18,
357+
"metadata": {},
358+
"outputs": [],
359+
"source": [
360+
"os.remove(os.path.join(\"..\",\"SampleDBs\",\"test.db\"))"
361+
]
362+
},
252363
{
253364
"cell_type": "markdown",
254365
"metadata": {},
@@ -277,7 +388,7 @@
277388
},
278389
{
279390
"cell_type": "code",
280-
"execution_count": 14,
391+
"execution_count": 19,
281392
"metadata": {},
282393
"outputs": [],
283394
"source": [
@@ -287,7 +398,7 @@
287398
},
288399
{
289400
"cell_type": "code",
290-
"execution_count": 15,
401+
"execution_count": 20,
291402
"metadata": {},
292403
"outputs": [
293404
{
@@ -329,7 +440,7 @@
329440
},
330441
{
331442
"cell_type": "code",
332-
"execution_count": 17,
443+
"execution_count": 21,
333444
"metadata": {},
334445
"outputs": [
335446
{
@@ -361,7 +472,7 @@
361472
},
362473
{
363474
"cell_type": "code",
364-
"execution_count": 18,
475+
"execution_count": 22,
365476
"metadata": {},
366477
"outputs": [
367478
{
@@ -395,7 +506,7 @@
395506
},
396507
{
397508
"cell_type": "code",
398-
"execution_count": 19,
509+
"execution_count": 23,
399510
"metadata": {},
400511
"outputs": [
401512
{
@@ -429,7 +540,7 @@
429540
},
430541
{
431542
"cell_type": "code",
432-
"execution_count": 20,
543+
"execution_count": 24,
433544
"metadata": {},
434545
"outputs": [
435546
{
@@ -463,7 +574,7 @@
463574
},
464575
{
465576
"cell_type": "code",
466-
"execution_count": 21,
577+
"execution_count": 25,
467578
"metadata": {},
468579
"outputs": [
469580
{
@@ -497,7 +608,7 @@
497608
},
498609
{
499610
"cell_type": "code",
500-
"execution_count": 22,
611+
"execution_count": 26,
501612
"metadata": {},
502613
"outputs": [
503614
{
@@ -526,7 +637,7 @@
526637
},
527638
{
528639
"cell_type": "code",
529-
"execution_count": 23,
640+
"execution_count": 27,
530641
"metadata": {},
531642
"outputs": [
532643
{
@@ -561,7 +672,7 @@
561672
},
562673
{
563674
"cell_type": "code",
564-
"execution_count": 24,
675+
"execution_count": 28,
565676
"metadata": {},
566677
"outputs": [
567678
{
@@ -595,7 +706,7 @@
595706
},
596707
{
597708
"cell_type": "code",
598-
"execution_count": 25,
709+
"execution_count": 29,
599710
"metadata": {},
600711
"outputs": [
601712
{
@@ -628,7 +739,7 @@
628739
},
629740
{
630741
"cell_type": "code",
631-
"execution_count": 26,
742+
"execution_count": 30,
632743
"metadata": {},
633744
"outputs": [
634745
{
@@ -663,7 +774,7 @@
663774
},
664775
{
665776
"cell_type": "code",
666-
"execution_count": 27,
777+
"execution_count": 31,
667778
"metadata": {},
668779
"outputs": [
669780
{
@@ -698,7 +809,7 @@
698809
},
699810
{
700811
"cell_type": "code",
701-
"execution_count": 28,
812+
"execution_count": 32,
702813
"metadata": {},
703814
"outputs": [
704815
{
@@ -734,7 +845,7 @@
734845
},
735846
{
736847
"cell_type": "code",
737-
"execution_count": 29,
848+
"execution_count": 33,
738849
"metadata": {},
739850
"outputs": [
740851
{
@@ -777,7 +888,7 @@
777888
},
778889
{
779890
"cell_type": "code",
780-
"execution_count": 31,
891+
"execution_count": 34,
781892
"metadata": {},
782893
"outputs": [
783894
{
@@ -851,7 +962,7 @@
851962
"4 Worlds "
852963
]
853964
},
854-
"execution_count": 31,
965+
"execution_count": 34,
855966
"metadata": {},
856967
"output_type": "execute_result"
857968
}
@@ -864,7 +975,7 @@
864975
],
865976
"metadata": {
866977
"kernelspec": {
867-
"display_name": "Python 3",
978+
"display_name": "Python 3 (ipykernel)",
868979
"language": "python",
869980
"name": "python3"
870981
},
@@ -878,7 +989,7 @@
878989
"name": "python",
879990
"nbconvert_exporter": "python",
880991
"pygments_lexer": "ipython3",
881-
"version": "3.7.6"
992+
"version": "3.8.10"
882993
}
883994
},
884995
"nbformat": 4,

SampleDBs/nursery_create.sql

100644100755
File mode changed.

0 commit comments

Comments
 (0)