|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Error Trapping for Database Errors" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [], |
| 15 | + "source": [ |
| 16 | + "import psycopg2" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "markdown", |
| 21 | + "metadata": {}, |
| 22 | + "source": [ |
| 23 | + "## Connecting with invalid credentials" |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": 3, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "USER = 'postgres'\n", |
| 33 | + "PASSWORD = 'someinvalidpassword'" |
| 34 | + ] |
| 35 | + }, |
| 36 | + { |
| 37 | + "cell_type": "code", |
| 38 | + "execution_count": 5, |
| 39 | + "metadata": {}, |
| 40 | + "outputs": [ |
| 41 | + { |
| 42 | + "ename": "OperationalError", |
| 43 | + "evalue": "FATAL: password authentication failed for user \"postgres\"\n", |
| 44 | + "output_type": "error", |
| 45 | + "traceback": [ |
| 46 | + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", |
| 47 | + "\u001b[1;31mOperationalError\u001b[0m Traceback (most recent call last)", |
| 48 | + "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m conn = psycopg2.connect(database='db_Advanced_SQL_Application_Development',\n\u001b[0m\u001b[0;32m 2\u001b[0m \u001b[0muser\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mUSER\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mpassword\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mPASSWORD\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m host='localhost')\n", |
| 49 | + "\u001b[1;32mC:\\ProgramData\\Anaconda3\\lib\\site-packages\\psycopg2\\__init__.py\u001b[0m in \u001b[0;36mconnect\u001b[1;34m(dsn, connection_factory, cursor_factory, **kwargs)\u001b[0m\n\u001b[0;32m 125\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 126\u001b[0m \u001b[0mdsn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_ext\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmake_dsn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdsn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 127\u001b[1;33m \u001b[0mconn\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_connect\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mdsn\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconnection_factory\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mconnection_factory\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwasync\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 128\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcursor_factory\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[1;32mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 129\u001b[0m \u001b[0mconn\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcursor_factory\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcursor_factory\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", |
| 50 | + "\u001b[1;31mOperationalError\u001b[0m: FATAL: password authentication failed for user \"postgres\"\n" |
| 51 | + ] |
| 52 | + } |
| 53 | + ], |
| 54 | + "source": [ |
| 55 | + "conn = psycopg2.connect(database='db_Advanced_SQL_Application_Development',\n", |
| 56 | + " user=USER,\n", |
| 57 | + " password=PASSWORD,\n", |
| 58 | + " host='localhost')" |
| 59 | + ] |
| 60 | + }, |
| 61 | + { |
| 62 | + "cell_type": "markdown", |
| 63 | + "metadata": {}, |
| 64 | + "source": [ |
| 65 | + "### Exception Handling" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "code", |
| 70 | + "execution_count": 7, |
| 71 | + "metadata": {}, |
| 72 | + "outputs": [ |
| 73 | + { |
| 74 | + "name": "stdout", |
| 75 | + "output_type": "stream", |
| 76 | + "text": [ |
| 77 | + "FATAL: password authentication failed for user \"postgres\"\n", |
| 78 | + "\n" |
| 79 | + ] |
| 80 | + } |
| 81 | + ], |
| 82 | + "source": [ |
| 83 | + "try:\n", |
| 84 | + " conn = psycopg2.connect(database='db_Advanced_SQL_Application_Development',\n", |
| 85 | + " user=USER,\n", |
| 86 | + " password=PASSWORD,\n", |
| 87 | + " host='localhost')\n", |
| 88 | + "except psycopg2.OperationalError as err:\n", |
| 89 | + " print(err)\n", |
| 90 | + " conn = None" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 9, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "None\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "print(conn)" |
| 108 | + ] |
| 109 | + }, |
| 110 | + { |
| 111 | + "cell_type": "markdown", |
| 112 | + "metadata": {}, |
| 113 | + "source": [ |
| 114 | + "----------" |
| 115 | + ] |
| 116 | + }, |
| 117 | + { |
| 118 | + "cell_type": "markdown", |
| 119 | + "metadata": {}, |
| 120 | + "source": [ |
| 121 | + "-----------" |
| 122 | + ] |
| 123 | + }, |
| 124 | + { |
| 125 | + "cell_type": "markdown", |
| 126 | + "metadata": {}, |
| 127 | + "source": [ |
| 128 | + "# Challenge" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "markdown", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "Your python web application is failing with an unhandled excpetion because an attempted connection to the database failed.\n", |
| 136 | + "\n", |
| 137 | + "- What Python programming statement would you use to trap the error?\n", |
| 138 | + "\n", |
| 139 | + "- What Algorithm would you use in the error handling code to attempt reconnects to the database?" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "# Solution" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "- try except clause\n", |
| 154 | + "- exceptional back off approach" |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": null, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [] |
| 163 | + } |
| 164 | + ], |
| 165 | + "metadata": { |
| 166 | + "kernelspec": { |
| 167 | + "display_name": "Python 3", |
| 168 | + "language": "python", |
| 169 | + "name": "python3" |
| 170 | + }, |
| 171 | + "language_info": { |
| 172 | + "codemirror_mode": { |
| 173 | + "name": "ipython", |
| 174 | + "version": 3 |
| 175 | + }, |
| 176 | + "file_extension": ".py", |
| 177 | + "mimetype": "text/x-python", |
| 178 | + "name": "python", |
| 179 | + "nbconvert_exporter": "python", |
| 180 | + "pygments_lexer": "ipython3", |
| 181 | + "version": "3.8.5" |
| 182 | + } |
| 183 | + }, |
| 184 | + "nbformat": 4, |
| 185 | + "nbformat_minor": 4 |
| 186 | +} |
0 commit comments