Skip to content

Commit 652ce63

Browse files
authored
Database Connection with Python using MSSQL_Server
1 parent 6d35797 commit 652ce63

File tree

1 file changed

+185
-0
lines changed

1 file changed

+185
-0
lines changed

mssqlserver.ipynb

Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"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",
8+
"\n",
9+
"1. Data Storage and Retrieval\n",
10+
"2. Enabling SQL Queries\n",
11+
"3. Transaction Management\n",
12+
"4. Concurrency and Multi-user Access\n",
13+
"5. Security and Authentication\n",
14+
"6. Efficiency and Performance\n",
15+
"7. Data Integrity and Consistency\n",
16+
"8. Communication Between Application and Database\n",
17+
"9. Managing Complex Queries and Relationships\n",
18+
"10. External Applications and APIs"
19+
]
20+
},
21+
{
22+
"cell_type": "markdown",
23+
"metadata": {},
24+
"source": [
25+
"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",
26+
"\n",
27+
"**1. Import required library:** To connect to a Microsoft SQL Server (MSSQL) database from Python, you can use the pyodbc library or pymssql. The most common choice is pyodbc because it provides a flexible and powerful way to work with MSSQL databases."
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 1,
33+
"metadata": {},
34+
"outputs": [
35+
{
36+
"name": "stdout",
37+
"output_type": "stream",
38+
"text": [
39+
"Requirement already satisfied: pyodbc in c:\\users\\91960\\machine learning\\database connection\\database_connection\\lib\\site-packages (5.2.0)\n"
40+
]
41+
},
42+
{
43+
"name": "stderr",
44+
"output_type": "stream",
45+
"text": [
46+
"\n",
47+
"[notice] A new release of pip is available: 23.2.1 -> 24.3.1\n",
48+
"[notice] To update, run: python.exe -m pip install --upgrade pip\n"
49+
]
50+
}
51+
],
52+
"source": [
53+
"!pip install pyodbc\n",
54+
"import pyodbc"
55+
]
56+
},
57+
{
58+
"cell_type": "markdown",
59+
"metadata": {},
60+
"source": [
61+
"**2. Define your connection parameter:** "
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"metadata": {},
68+
"outputs": [],
69+
"source": [
70+
"server = 'your_server_name'\n",
71+
"database = 'your_database_name'\n",
72+
"username = 'your_username'\n",
73+
"password = 'your_password'"
74+
]
75+
},
76+
{
77+
"cell_type": "markdown",
78+
"metadata": {},
79+
"source": [
80+
"**3. Create the Connection String:**"
81+
]
82+
},
83+
{
84+
"cell_type": "code",
85+
"execution_count": null,
86+
"metadata": {},
87+
"outputs": [],
88+
"source": [
89+
"conn_str = (\n",
90+
" r'DRIVER={ODBC Driver 17 for SQL Server};'\n",
91+
" r'SERVER=' + server + ';'\n",
92+
" r'DATABASE=' + database + ';'\n",
93+
" r'UID=' + username + ';'\n",
94+
" r'PWD=' + password\n",
95+
")"
96+
]
97+
},
98+
{
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"**4. Connect to the Database:**"
103+
]
104+
},
105+
{
106+
"cell_type": "code",
107+
"execution_count": null,
108+
"metadata": {},
109+
"outputs": [],
110+
"source": [
111+
"try:\n",
112+
" conn = pyodbc.connect(conn_str)\n",
113+
" print(\"Connected to the database successfully!\")\n",
114+
" \n",
115+
" # Create a cursor object to interact with the database\n",
116+
" cursor = conn.cursor()\n",
117+
"\n",
118+
" # Example: Query the database\n",
119+
" cursor.execute('SELECT @@VERSION')\n",
120+
" row = cursor.fetchone()\n",
121+
" print(\"Database version:\", row[0])\n",
122+
"\n",
123+
" # Example: Insert data into a table\n",
124+
" cursor.execute(\"INSERT INTO users (name) VALUES ('John Doe')\")\n",
125+
" conn.commit() # Commit the transaction\n",
126+
"\n",
127+
" # Example: Retrieve data\n",
128+
" cursor.execute(\"SELECT * FROM users\")\n",
129+
" rows = cursor.fetchall()\n",
130+
" for row in rows:\n",
131+
" print(row)\n",
132+
"\n",
133+
"except Exception as e:\n",
134+
" print(f\"Error: {e}\")\n",
135+
"\n",
136+
"finally:\n",
137+
" # Always close the connection after use\n",
138+
" if conn:\n",
139+
" conn.close()"
140+
]
141+
},
142+
{
143+
"cell_type": "markdown",
144+
"metadata": {},
145+
"source": [
146+
"**5. Querying:**"
147+
]
148+
},
149+
{
150+
"cell_type": "code",
151+
"execution_count": null,
152+
"metadata": {},
153+
"outputs": [],
154+
"source": [
155+
"cursor.execute('SELECT * FROM your_table_name')\n",
156+
"\n",
157+
"rows = cursor.fetchall()\n",
158+
"\n",
159+
"for row in rows:\n",
160+
" print(row)"
161+
]
162+
}
163+
],
164+
"metadata": {
165+
"kernelspec": {
166+
"display_name": "database_connection",
167+
"language": "python",
168+
"name": "python3"
169+
},
170+
"language_info": {
171+
"codemirror_mode": {
172+
"name": "ipython",
173+
"version": 3
174+
},
175+
"file_extension": ".py",
176+
"mimetype": "text/x-python",
177+
"name": "python",
178+
"nbconvert_exporter": "python",
179+
"pygments_lexer": "ipython3",
180+
"version": "3.12.1"
181+
}
182+
},
183+
"nbformat": 4,
184+
"nbformat_minor": 2
185+
}

0 commit comments

Comments
 (0)