|
| 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": 20, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "server = 'NITIN'\n", |
| 71 | + "database = 'DB_Connection'" |
| 72 | + ] |
| 73 | + }, |
| 74 | + { |
| 75 | + "cell_type": "markdown", |
| 76 | + "metadata": {}, |
| 77 | + "source": [ |
| 78 | + "**3. Create the Connection String:**" |
| 79 | + ] |
| 80 | + }, |
| 81 | + { |
| 82 | + "cell_type": "code", |
| 83 | + "execution_count": 21, |
| 84 | + "metadata": {}, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "conn = pyodbc.connect(f'DRIVER={{ODBC Driver 17 for SQL Server}};'\n", |
| 88 | + " f'SERVER={server};'\n", |
| 89 | + " f'DATABASE={database};'\n", |
| 90 | + " f'Trusted_Connection=yes')" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "markdown", |
| 95 | + "metadata": {}, |
| 96 | + "source": [ |
| 97 | + "**4. Creating a Cursor:**" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "code", |
| 102 | + "execution_count": 22, |
| 103 | + "metadata": {}, |
| 104 | + "outputs": [], |
| 105 | + "source": [ |
| 106 | + "cursor = conn.cursor()" |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "markdown", |
| 111 | + "metadata": {}, |
| 112 | + "source": [ |
| 113 | + "**5. Creating a Table:**" |
| 114 | + ] |
| 115 | + }, |
| 116 | + { |
| 117 | + "cell_type": "code", |
| 118 | + "execution_count": 23, |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "name": "stdout", |
| 123 | + "output_type": "stream", |
| 124 | + "text": [ |
| 125 | + "Table Created Successfully.\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "cursor.execute('CREATE TABLE Testing_Connection (Test_ID INT, Test_Name VARCHAR(50))')\n", |
| 131 | + "\n", |
| 132 | + "conn.commit()\n", |
| 133 | + "\n", |
| 134 | + "print(\"Table Created Successfully.\")" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "metadata": {}, |
| 140 | + "source": [ |
| 141 | + "**6. Inserting Data to the Table:**" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "code", |
| 146 | + "execution_count": 24, |
| 147 | + "metadata": {}, |
| 148 | + "outputs": [ |
| 149 | + { |
| 150 | + "name": "stdout", |
| 151 | + "output_type": "stream", |
| 152 | + "text": [ |
| 153 | + "Row Inserted.\n" |
| 154 | + ] |
| 155 | + } |
| 156 | + ], |
| 157 | + "source": [ |
| 158 | + "cursor.execute(\"INSERT INTO Testing_Connection (test_ID, Test_Name) VALUES (1, 'Nitin Testing')\")\n", |
| 159 | + "\n", |
| 160 | + "conn.commit()\n", |
| 161 | + "\n", |
| 162 | + "print('Row Inserted.')" |
| 163 | + ] |
| 164 | + }, |
| 165 | + { |
| 166 | + "cell_type": "markdown", |
| 167 | + "metadata": {}, |
| 168 | + "source": [ |
| 169 | + "**7. Fetch and Display Results:**" |
| 170 | + ] |
| 171 | + }, |
| 172 | + { |
| 173 | + "cell_type": "code", |
| 174 | + "execution_count": 26, |
| 175 | + "metadata": {}, |
| 176 | + "outputs": [ |
| 177 | + { |
| 178 | + "name": "stdout", |
| 179 | + "output_type": "stream", |
| 180 | + "text": [ |
| 181 | + "(1, 'Nitin Testing')\n" |
| 182 | + ] |
| 183 | + } |
| 184 | + ], |
| 185 | + "source": [ |
| 186 | + "cursor.execute(\"SELECT * FROM Testing_Connection\")\n", |
| 187 | + "\n", |
| 188 | + "rows = cursor.fetchall()\n", |
| 189 | + "\n", |
| 190 | + "for row in rows:\n", |
| 191 | + " print(row)" |
| 192 | + ] |
| 193 | + }, |
| 194 | + { |
| 195 | + "cell_type": "markdown", |
| 196 | + "metadata": {}, |
| 197 | + "source": [ |
| 198 | + "**8. Close Cursor and Connection:**" |
| 199 | + ] |
| 200 | + }, |
| 201 | + { |
| 202 | + "cell_type": "code", |
| 203 | + "execution_count": null, |
| 204 | + "metadata": {}, |
| 205 | + "outputs": [], |
| 206 | + "source": [ |
| 207 | + "cursor.close()\n", |
| 208 | + "conn.close()" |
| 209 | + ] |
| 210 | + } |
| 211 | + ], |
| 212 | + "metadata": { |
| 213 | + "kernelspec": { |
| 214 | + "display_name": "database_connection", |
| 215 | + "language": "python", |
| 216 | + "name": "python3" |
| 217 | + }, |
| 218 | + "language_info": { |
| 219 | + "codemirror_mode": { |
| 220 | + "name": "ipython", |
| 221 | + "version": 3 |
| 222 | + }, |
| 223 | + "file_extension": ".py", |
| 224 | + "mimetype": "text/x-python", |
| 225 | + "name": "python", |
| 226 | + "nbconvert_exporter": "python", |
| 227 | + "pygments_lexer": "ipython3", |
| 228 | + "version": "3.12.1" |
| 229 | + } |
| 230 | + }, |
| 231 | + "nbformat": 4, |
| 232 | + "nbformat_minor": 2 |
| 233 | +} |
0 commit comments