Skip to content

Commit 5d087dc

Browse files
authored
Database Connection with Python using MySQL
1 parent 9838839 commit 5d087dc

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

mysql.ipynb

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Database Connection with Python using MySQL"
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:** You'll need to install a MySQL driver like mysql-connector-python or PyMySQL."
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"metadata": {},
43+
"outputs": [],
44+
"source": [
45+
"!pip install mysql-connector-python\n",
46+
"import mysql.connector"
47+
]
48+
},
49+
{
50+
"cell_type": "markdown",
51+
"metadata": {},
52+
"source": [
53+
"**2. Establish the connection to MySQL Database:** "
54+
]
55+
},
56+
{
57+
"cell_type": "code",
58+
"execution_count": null,
59+
"metadata": {},
60+
"outputs": [],
61+
"source": [
62+
"connect = mysql.connector.connect(\n",
63+
" host='localhost',\n",
64+
" user='your_username',\n",
65+
" password='your_password',\n",
66+
" database='your_database'\n",
67+
")"
68+
]
69+
},
70+
{
71+
"cell_type": "markdown",
72+
"metadata": {},
73+
"source": [
74+
"**3. Create Cursor Object using the connection:**"
75+
]
76+
},
77+
{
78+
"cell_type": "code",
79+
"execution_count": null,
80+
"metadata": {},
81+
"outputs": [],
82+
"source": [
83+
"cursor = connect.cursor()"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"**4. Create Table:**"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {},
97+
"outputs": [],
98+
"source": [
99+
"cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100))''')"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"**5. Insert Data:**"
107+
]
108+
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"cursor.execute(\"INSERT INTO users (name) VALUES ('nitin')\")\n",
116+
"cursor.execute(\"INSERT INTO users (name) VALUES ('rucha')\")\n",
117+
"cursor.execute(\"INSERT INTO users (name) VALUES ('neha')\")\n",
118+
"cursor.execute(\"INSERT INTO users (name) VALUES ('timish')\")\n",
119+
"cursor.execute(\"INSERT INTO users (name) VALUES ('amol')\")"
120+
]
121+
},
122+
{
123+
"cell_type": "markdown",
124+
"metadata": {},
125+
"source": [
126+
"**6. Commit the Changes:**"
127+
]
128+
},
129+
{
130+
"cell_type": "code",
131+
"execution_count": null,
132+
"metadata": {},
133+
"outputs": [],
134+
"source": [
135+
"connect.commit()"
136+
]
137+
},
138+
{
139+
"cell_type": "markdown",
140+
"metadata": {},
141+
"source": [
142+
"**7. Retrieve Data:**"
143+
]
144+
},
145+
{
146+
"cell_type": "code",
147+
"execution_count": null,
148+
"metadata": {},
149+
"outputs": [],
150+
"source": [
151+
"cursor.execute(\"SELECT * FROM users\")\n",
152+
"\n",
153+
"rows = cursor.fetchall\n",
154+
"\n",
155+
"for row in rows:\n",
156+
" print(row)"
157+
]
158+
},
159+
{
160+
"cell_type": "markdown",
161+
"metadata": {},
162+
"source": [
163+
"**8. Close the connection:**"
164+
]
165+
},
166+
{
167+
"cell_type": "code",
168+
"execution_count": null,
169+
"metadata": {},
170+
"outputs": [],
171+
"source": [
172+
"cursor.close()\n",
173+
"connect.close()"
174+
]
175+
}
176+
],
177+
"metadata": {
178+
"kernelspec": {
179+
"display_name": "database_connection",
180+
"language": "python",
181+
"name": "python3"
182+
},
183+
"language_info": {
184+
"name": "python",
185+
"version": "3.12.1"
186+
}
187+
},
188+
"nbformat": 4,
189+
"nbformat_minor": 2
190+
}

0 commit comments

Comments
 (0)