Skip to content

Commit 9838839

Browse files
authored
Database Connection with Python using SQLite
1 parent 7617a3b commit 9838839

File tree

1 file changed

+291
-0
lines changed

1 file changed

+291
-0
lines changed

sqlite.ipynb

Lines changed: 291 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,291 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Database Connection with Python using SQLite3"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"A database connection is essential for allowing your application (such as a Python program) to interact with a database. Without a connection, your program wouldn't be able to read, write, or manipulate data stored in the database. Here are some of the key reasons why a database connection is needed:\n",
15+
"\n",
16+
"1. Data Storage and Retrieval\n",
17+
"2. Enabling SQL Queries\n",
18+
"3. Transaction Management\n",
19+
"4. Concurrency and Multi-user Access\n",
20+
"5. Security and Authentication\n",
21+
"6. Efficiency and Performance\n",
22+
"7. Data Integrity and Consistency\n",
23+
"8. Communication Between Application and Database\n",
24+
"9. Managing Complex Queries and Relationships\n",
25+
"10. External Applications and APIs"
26+
]
27+
},
28+
{
29+
"cell_type": "markdown",
30+
"metadata": {},
31+
"source": [
32+
"### How to do Database Connection\n",
33+
"\n",
34+
"To connect to a database using Python, you'll need to use a database connector or driver specific to the type of database you're working with (e.g., SQLite, MySQL, PostgreSQL).\n",
35+
"\n",
36+
"**1. Import required library (SQLite):** SQLite is a lightweight, serverless database, and Python has a built-in library called sqlite3 to connect to SQLite databases."
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": 23,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"import sqlite3"
46+
]
47+
},
48+
{
49+
"cell_type": "markdown",
50+
"metadata": {},
51+
"source": [
52+
"**2. Connect to a Database:** It creates a new one if it doesn't exist."
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": 24,
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"connect = sqlite3.connect('example.db')"
62+
]
63+
},
64+
{
65+
"cell_type": "markdown",
66+
"metadata": {},
67+
"source": [
68+
"**3. Create a Cursor Object:** Cursor serves as an intermediary between your Python code and the database."
69+
]
70+
},
71+
{
72+
"cell_type": "code",
73+
"execution_count": 25,
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"cursor = connect.cursor()"
78+
]
79+
},
80+
{
81+
"cell_type": "markdown",
82+
"metadata": {},
83+
"source": [
84+
"**4. Create a Table:** Create a new table if doesn't exist."
85+
]
86+
},
87+
{
88+
"cell_type": "code",
89+
"execution_count": 26,
90+
"metadata": {},
91+
"outputs": [
92+
{
93+
"data": {
94+
"text/plain": [
95+
"<sqlite3.Cursor at 0x1abd038dec0>"
96+
]
97+
},
98+
"execution_count": 26,
99+
"metadata": {},
100+
"output_type": "execute_result"
101+
}
102+
],
103+
"source": [
104+
"cursor.execute('''CREATE TABLE IF NOT EXISTS person (id INTEGER PRIMARY KEY, name VARCHAR NOT NULL, age INTEGER, department VARCHAR)''')"
105+
]
106+
},
107+
{
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"**5. Insert data into Tables:** "
112+
]
113+
},
114+
{
115+
"cell_type": "code",
116+
"execution_count": 27,
117+
"metadata": {},
118+
"outputs": [
119+
{
120+
"data": {
121+
"text/plain": [
122+
"<sqlite3.Cursor at 0x1abd038dec0>"
123+
]
124+
},
125+
"execution_count": 27,
126+
"metadata": {},
127+
"output_type": "execute_result"
128+
}
129+
],
130+
"source": [
131+
"cursor.execute(\"INSERT INTO person (name, age, department) VALUES ('nitin', 28, 'Machine Learning')\")\n",
132+
"\n",
133+
"cursor.execute(\"INSERT INTO person (name, age, department) VALUES ('ruchita', 30, 'Data Analyst')\")\n",
134+
"\n",
135+
"cursor.execute(\"INSERT INTO person (name, age, department) VALUES ('neha', 29, 'Data Engineer')\")\n",
136+
"\n",
137+
"cursor.execute(\"INSERT INTO person (name, age, department) VALUES ('timish', 27, 'Cloud Engineer')\")\n",
138+
"\n",
139+
"cursor.execute(\"INSERT INTO person (name, age, department) VALUES ('amol', 27, 'Testing Engineer')\")"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"**6. Commit the changes:** "
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": 28,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"connect.commit()"
156+
]
157+
},
158+
{
159+
"cell_type": "markdown",
160+
"metadata": {},
161+
"source": [
162+
"**7. Retrieve Data:**"
163+
]
164+
},
165+
{
166+
"cell_type": "code",
167+
"execution_count": 29,
168+
"metadata": {},
169+
"outputs": [
170+
{
171+
"name": "stdout",
172+
"output_type": "stream",
173+
"text": [
174+
"(1, 'nitin', 28, 'Machine Learning')\n",
175+
"(2, 'ruchita', 30, 'Data Analyst')\n",
176+
"(3, 'neha', 29, 'Data Engineer')\n",
177+
"(4, 'timish', 27, 'Cloud Engineer')\n",
178+
"(5, 'amol', 27, 'Testing Engineer')\n"
179+
]
180+
}
181+
],
182+
"source": [
183+
"cursor.execute(\"SELECT * FROM person\")\n",
184+
"\n",
185+
"rows = cursor.fetchall()\n",
186+
"\n",
187+
"for row in rows:\n",
188+
" print(row)"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"**8. Close the Connecction:**"
196+
]
197+
},
198+
{
199+
"cell_type": "code",
200+
"execution_count": 30,
201+
"metadata": {},
202+
"outputs": [],
203+
"source": [
204+
"connect.close()"
205+
]
206+
},
207+
{
208+
"cell_type": "markdown",
209+
"metadata": {},
210+
"source": [
211+
"### Full Example:"
212+
]
213+
},
214+
{
215+
"cell_type": "code",
216+
"execution_count": 22,
217+
"metadata": {},
218+
"outputs": [
219+
{
220+
"name": "stdout",
221+
"output_type": "stream",
222+
"text": [
223+
"(1, 'nitin', 28, 'Machine Learning')\n",
224+
"(2, 'rucha', 30, 'Data Analyst')\n",
225+
"(3, 'neha', 29, 'Data Engineer')\n",
226+
"(4, 'timish', 27, 'Cloud Engineer')\n",
227+
"(5, 'amol', 27, 'Testing Engineer')\n"
228+
]
229+
}
230+
],
231+
"source": [
232+
"import sqlite3\n",
233+
"\n",
234+
"# Connect to a database (it creates a new one if it doesn't exist)\n",
235+
"connect = sqlite3.connect('example.db')\n",
236+
"\n",
237+
"# Create a cursor object\n",
238+
"cursor = connect.cursor()\n",
239+
"\n",
240+
"# Create a table (if it doesn't exist)\n",
241+
"cursor.execute('''CREATE TABLE IF NOT EXISTS students (\n",
242+
" id INTEGER PRIMARY KEY, \n",
243+
" name TEXT, \n",
244+
" age INTEGER, \n",
245+
" department TEXT)''')\n",
246+
"\n",
247+
"# Insert data into the table\n",
248+
"cursor.execute(\"INSERT INTO students (name, age, department) VALUES ('nitin', 28, 'Machine Learning')\")\n",
249+
"cursor.execute(\"INSERT INTO students (name, age, department) VALUES ('rucha', 30, 'Data Analyst')\")\n",
250+
"cursor.execute(\"INSERT INTO students (name, age, department) VALUES ('neha', 29, 'Data Engineer')\")\n",
251+
"cursor.execute(\"INSERT INTO students (name, age, department) VALUES ('timish', 27, 'Cloud Engineer')\")\n",
252+
"cursor.execute(\"INSERT INTO students (name, age, department) VALUES ('amol', 27, 'Testing Engineer')\")\n",
253+
"\n",
254+
"# Commit the changes\n",
255+
"connect.commit()\n",
256+
"\n",
257+
"# Retrieve data\n",
258+
"cursor.execute(\"SELECT * FROM students\")\n",
259+
"rows = cursor.fetchall()\n",
260+
"\n",
261+
"# Display the results\n",
262+
"for row in rows:\n",
263+
" print(row)\n",
264+
"\n",
265+
"# Close the connection\n",
266+
"connect.close()\n"
267+
]
268+
}
269+
],
270+
"metadata": {
271+
"kernelspec": {
272+
"display_name": "database_connection",
273+
"language": "python",
274+
"name": "python3"
275+
},
276+
"language_info": {
277+
"codemirror_mode": {
278+
"name": "ipython",
279+
"version": 3
280+
},
281+
"file_extension": ".py",
282+
"mimetype": "text/x-python",
283+
"name": "python",
284+
"nbconvert_exporter": "python",
285+
"pygments_lexer": "ipython3",
286+
"version": "3.12.1"
287+
}
288+
},
289+
"nbformat": 4,
290+
"nbformat_minor": 2
291+
}

0 commit comments

Comments
 (0)