Skip to content

Commit c6a7db6

Browse files
authored
Add files via upload
1 parent c3ea9f0 commit c6a7db6

File tree

3 files changed

+361
-0
lines changed

3 files changed

+361
-0
lines changed
Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### SQL And SQLite\n",
8+
"SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. SQLite is a self-contained, serverless, and zero-configuration database engine that is widely used for embedded database systems. In this lesson, we will cover the basics of SQL and SQLite, including creating databases, tables, and performing various SQL operations."
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 1,
14+
"metadata": {},
15+
"outputs": [],
16+
"source": [
17+
"import sqlite3"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 3,
23+
"metadata": {},
24+
"outputs": [
25+
{
26+
"data": {
27+
"text/plain": [
28+
"<sqlite3.Connection at 0x20061df8d60>"
29+
]
30+
},
31+
"execution_count": 3,
32+
"metadata": {},
33+
"output_type": "execute_result"
34+
}
35+
],
36+
"source": [
37+
"## Connect to an SQLite database\n",
38+
"connection=sqlite3.connect('example.db')\n",
39+
"connection"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 5,
45+
"metadata": {},
46+
"outputs": [],
47+
"source": [
48+
"cursor=connection.cursor()"
49+
]
50+
},
51+
{
52+
"cell_type": "code",
53+
"execution_count": 6,
54+
"metadata": {},
55+
"outputs": [],
56+
"source": [
57+
"## Create a Table\n",
58+
"cursor.execute('''\n",
59+
"Create Table If Not Exists employees(\n",
60+
" id Integer Primary Key,\n",
61+
" name Text Not Null,\n",
62+
" age Integer,\n",
63+
" department text\n",
64+
" )\n",
65+
"''')\n",
66+
"\n",
67+
"## Commit the changes\n",
68+
"connection.commit()"
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 8,
74+
"metadata": {},
75+
"outputs": [
76+
{
77+
"data": {
78+
"text/plain": [
79+
"<sqlite3.Cursor at 0x200619658c0>"
80+
]
81+
},
82+
"execution_count": 8,
83+
"metadata": {},
84+
"output_type": "execute_result"
85+
}
86+
],
87+
"source": [
88+
"cursor.execute('''\n",
89+
"Select * from employees\n",
90+
" \n",
91+
"''')"
92+
]
93+
},
94+
{
95+
"cell_type": "code",
96+
"execution_count": 9,
97+
"metadata": {},
98+
"outputs": [],
99+
"source": [
100+
"## Insert the data in sqlite table\n",
101+
"cursor.execute('''\n",
102+
"Insert Into employees(name,age,department)\n",
103+
" values('Krish',32,'Data Scientist')\n",
104+
"\n",
105+
"''')\n",
106+
"\n",
107+
"cursor.execute('''\n",
108+
"INSERT INTO employees (name, age, department)\n",
109+
"VALUES ('Bob', 25, 'Engineering')\n",
110+
"''')\n",
111+
"\n",
112+
"cursor.execute('''\n",
113+
"INSERT INTO employees (name, age, department)\n",
114+
"VALUES ('Charlie', 35, 'Finance')\n",
115+
"''')\n",
116+
"\n",
117+
"## commi the changes\n",
118+
"connection.commit()"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 10,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
"(1, 'Krish', 32, 'Data Scientist')\n",
131+
"(2, 'Bob', 25, 'Engineering')\n",
132+
"(3, 'Charlie', 35, 'Finance')\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"## Query the data from the table\n",
138+
"cursor.execute('Select * from employees')\n",
139+
"rows=cursor.fetchall()\n",
140+
"\n",
141+
"## print the queried data\n",
142+
"\n",
143+
"for row in rows:\n",
144+
" print(row)"
145+
]
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": 12,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": [
153+
"## Update the data in the table\n",
154+
"cursor.execute('''\n",
155+
"UPDATE employees\n",
156+
"Set age=34\n",
157+
"where name=\"Krish\"\n",
158+
"''')\n",
159+
"\n",
160+
"connection.commit()"
161+
]
162+
},
163+
{
164+
"cell_type": "code",
165+
"execution_count": 13,
166+
"metadata": {},
167+
"outputs": [
168+
{
169+
"name": "stdout",
170+
"output_type": "stream",
171+
"text": [
172+
"(1, 'Krish', 34, 'Data Scientist')\n",
173+
"(2, 'Bob', 25, 'Engineering')\n",
174+
"(3, 'Charlie', 35, 'Finance')\n"
175+
]
176+
}
177+
],
178+
"source": [
179+
"## Query the data from the table\n",
180+
"cursor.execute('Select * from employees')\n",
181+
"rows=cursor.fetchall()\n",
182+
"\n",
183+
"## print the queried data\n",
184+
"\n",
185+
"for row in rows:\n",
186+
" print(row)"
187+
]
188+
},
189+
{
190+
"cell_type": "code",
191+
"execution_count": 16,
192+
"metadata": {},
193+
"outputs": [],
194+
"source": [
195+
"## Delete the data from the table\n",
196+
"cursor.execute('''\n",
197+
"Delete from employees\n",
198+
" where name ='Bob'\n",
199+
"''')\n",
200+
"\n",
201+
"connection.commit()"
202+
]
203+
},
204+
{
205+
"cell_type": "code",
206+
"execution_count": 17,
207+
"metadata": {},
208+
"outputs": [
209+
{
210+
"name": "stdout",
211+
"output_type": "stream",
212+
"text": [
213+
"(1, 'Krish', 34, 'Data Scientist')\n",
214+
"(3, 'Charlie', 35, 'Finance')\n"
215+
]
216+
}
217+
],
218+
"source": [
219+
"## Query the data from the table\n",
220+
"cursor.execute('Select * from employees')\n",
221+
"rows=cursor.fetchall()\n",
222+
"\n",
223+
"## print the queried data\n",
224+
"\n",
225+
"for row in rows:\n",
226+
" print(row)"
227+
]
228+
},
229+
{
230+
"cell_type": "code",
231+
"execution_count": 19,
232+
"metadata": {},
233+
"outputs": [],
234+
"source": [
235+
"## Working Wwith Sales Data\n",
236+
"# Connect to an SQLite database\n",
237+
"connection = sqlite3.connect('sales_data.db')\n",
238+
"cursor = connection.cursor()\n",
239+
"\n",
240+
"# Create a table for sales data\n",
241+
"cursor.execute('''\n",
242+
"CREATE TABLE IF NOT EXISTS sales (\n",
243+
" id INTEGER PRIMARY KEY,\n",
244+
" date TEXT NOT NULL,\n",
245+
" product TEXT NOT NULL,\n",
246+
" sales INTEGER,\n",
247+
" region TEXT\n",
248+
")\n",
249+
"''')\n",
250+
"\n",
251+
"# Insert data into the sales table\n",
252+
"sales_data = [\n",
253+
" ('2023-01-01', 'Product1', 100, 'North'),\n",
254+
" ('2023-01-02', 'Product2', 200, 'South'),\n",
255+
" ('2023-01-03', 'Product1', 150, 'East'),\n",
256+
" ('2023-01-04', 'Product3', 250, 'West'),\n",
257+
" ('2023-01-05', 'Product2', 300, 'North')\n",
258+
"]\n",
259+
"\n",
260+
"cursor.executemany('''\n",
261+
"Insert into sales(date,product,sales,region)\n",
262+
" values(?,?,?,?)\n",
263+
"''',sales_data)\n",
264+
"\n",
265+
"connection.commit()"
266+
]
267+
},
268+
{
269+
"cell_type": "code",
270+
"execution_count": 20,
271+
"metadata": {},
272+
"outputs": [
273+
{
274+
"name": "stdout",
275+
"output_type": "stream",
276+
"text": [
277+
"(1, '2023-01-01', 'Product1', 100, 'North')\n",
278+
"(2, '2023-01-02', 'Product2', 200, 'South')\n",
279+
"(3, '2023-01-03', 'Product1', 150, 'East')\n",
280+
"(4, '2023-01-04', 'Product3', 250, 'West')\n",
281+
"(5, '2023-01-05', 'Product2', 300, 'North')\n"
282+
]
283+
}
284+
],
285+
"source": [
286+
"# Query data from the sales table\n",
287+
"cursor.execute('SELECT * FROM sales')\n",
288+
"rows = cursor.fetchall()\n",
289+
"\n",
290+
"# Print the queried data\n",
291+
"for row in rows:\n",
292+
" print(row)"
293+
]
294+
},
295+
{
296+
"cell_type": "code",
297+
"execution_count": 21,
298+
"metadata": {},
299+
"outputs": [],
300+
"source": [
301+
"## close the connection\n",
302+
"connection.close()"
303+
]
304+
},
305+
{
306+
"cell_type": "code",
307+
"execution_count": 22,
308+
"metadata": {},
309+
"outputs": [
310+
{
311+
"ename": "ProgrammingError",
312+
"evalue": "Cannot operate on a closed database.",
313+
"output_type": "error",
314+
"traceback": [
315+
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
316+
"\u001b[1;31mProgrammingError\u001b[0m Traceback (most recent call last)",
317+
"Cell \u001b[1;32mIn[22], line 2\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Query data from the sales table\u001b[39;00m\n\u001b[1;32m----> 2\u001b[0m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[38;5;124;43mSELECT * FROM sales\u001b[39;49m\u001b[38;5;124;43m'\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[0;32m 3\u001b[0m rows \u001b[38;5;241m=\u001b[39m cursor\u001b[38;5;241m.\u001b[39mfetchall()\n\u001b[0;32m 5\u001b[0m \u001b[38;5;66;03m# Print the queried data\u001b[39;00m\n",
318+
"\u001b[1;31mProgrammingError\u001b[0m: Cannot operate on a closed database."
319+
]
320+
}
321+
],
322+
"source": [
323+
"# Query data from the sales table\n",
324+
"cursor.execute('SELECT * FROM sales')\n",
325+
"rows = cursor.fetchall()\n",
326+
"\n",
327+
"# Print the queried data\n",
328+
"for row in rows:\n",
329+
" print(row)"
330+
]
331+
},
332+
{
333+
"cell_type": "code",
334+
"execution_count": null,
335+
"metadata": {},
336+
"outputs": [],
337+
"source": []
338+
}
339+
],
340+
"metadata": {
341+
"kernelspec": {
342+
"display_name": "Python 3",
343+
"language": "python",
344+
"name": "python3"
345+
},
346+
"language_info": {
347+
"codemirror_mode": {
348+
"name": "ipython",
349+
"version": 3
350+
},
351+
"file_extension": ".py",
352+
"mimetype": "text/x-python",
353+
"name": "python",
354+
"nbconvert_exporter": "python",
355+
"pygments_lexer": "ipython3",
356+
"version": "3.12.0"
357+
}
358+
},
359+
"nbformat": 4,
360+
"nbformat_minor": 2
361+
}
8 KB
Binary file not shown.
8 KB
Binary file not shown.

0 commit comments

Comments
 (0)