|
2 | 2 | "cells": [ |
3 | 3 | { |
4 | 4 | "cell_type": "markdown", |
5 | | - "id": "909edcb0", |
| 5 | + "id": "2ec6e0e7", |
6 | 6 | "metadata": {}, |
7 | 7 | "source": [ |
8 | | - "## Using PyMySQL to access and modify RDS MySQL Database Instance" |
| 8 | + "# Using PyMySQL to access and modify RDS MySQL Database Instance" |
9 | 9 | ] |
10 | 10 | }, |
11 | 11 | { |
12 | 12 | "cell_type": "markdown", |
13 | | - "id": "f4e125bd", |
| 13 | + "id": "29d02d2b", |
14 | 14 | "metadata": {}, |
15 | 15 | "source": [ |
16 | 16 | "### Necessary imports" |
|
19 | 19 | { |
20 | 20 | "cell_type": "code", |
21 | 21 | "execution_count": 1, |
22 | | - "id": "f70c9d8c", |
| 22 | + "id": "f4706480", |
23 | 23 | "metadata": {}, |
24 | 24 | "outputs": [], |
25 | 25 | "source": [ |
|
29 | 29 | }, |
30 | 30 | { |
31 | 31 | "cell_type": "markdown", |
32 | | - "id": "9f6288a7", |
| 32 | + "id": "5ea1c5d0", |
33 | 33 | "metadata": {}, |
34 | 34 | "source": [ |
35 | 35 | "___\n", |
|
38 | 38 | }, |
39 | 39 | { |
40 | 40 | "cell_type": "code", |
41 | | - "execution_count": null, |
42 | | - "id": "caded1e3", |
| 41 | + "execution_count": 2, |
| 42 | + "id": "66a73d8e", |
43 | 43 | "metadata": {}, |
44 | 44 | "outputs": [], |
45 | 45 | "source": [ |
|
52 | 52 | " db=DBNAME,\n", |
53 | 53 | " cursorclass=CURSORCLASS,\n", |
54 | 54 | " ssl_ca=SSL_CA)\n", |
55 | | - " status = 'Success'\n", |
56 | 55 | " print('[+] RDS Connection Successful')\n", |
57 | 56 | " except Exception as e:\n", |
58 | | - " print(f'[+] RDS Connection Failed: {e}')\n", |
59 | | - " status, connection = 'Failed', None\n", |
| 57 | + " print(f'[-] RDS Connection Failed: {e}')\n", |
| 58 | + " connection = None\n", |
60 | 59 | "\n", |
61 | | - " return connection, status" |
| 60 | + " return connection" |
62 | 61 | ] |
63 | 62 | }, |
64 | 63 | { |
65 | 64 | "cell_type": "code", |
66 | | - "execution_count": null, |
67 | | - "id": "2e6db776", |
| 65 | + "execution_count": 3, |
| 66 | + "id": "961eb394", |
68 | 67 | "metadata": {}, |
69 | | - "outputs": [], |
| 68 | + "outputs": [ |
| 69 | + { |
| 70 | + "name": "stdout", |
| 71 | + "output_type": "stream", |
| 72 | + "text": [ |
| 73 | + "[+] RDS Connection Successful\n" |
| 74 | + ] |
| 75 | + } |
| 76 | + ], |
70 | 77 | "source": [ |
71 | 78 | "# Initiate RDS connection\n", |
72 | | - "connection, status = start_rds_connection()" |
| 79 | + "connection = start_rds_connection()" |
73 | 80 | ] |
74 | 81 | }, |
75 | 82 | { |
76 | 83 | "cell_type": "markdown", |
77 | | - "id": "42adf77b", |
| 84 | + "id": "49ffd330", |
78 | 85 | "metadata": {}, |
79 | 86 | "source": [ |
80 | 87 | "___\n", |
|
83 | 90 | }, |
84 | 91 | { |
85 | 92 | "cell_type": "code", |
86 | | - "execution_count": null, |
87 | | - "id": "db24605f", |
| 93 | + "execution_count": 4, |
| 94 | + "id": "dc36379b", |
88 | 95 | "metadata": {}, |
89 | 96 | "outputs": [], |
90 | 97 | "source": [ |
|
97 | 104 | }, |
98 | 105 | { |
99 | 106 | "cell_type": "code", |
100 | | - "execution_count": null, |
101 | | - "id": "361c09da", |
| 107 | + "execution_count": 5, |
| 108 | + "id": "66a455b6", |
102 | 109 | "metadata": {}, |
103 | 110 | "outputs": [], |
104 | 111 | "source": [ |
|
108 | 115 | " sql = f\"INSERT INTO `{tableName}` (`nClientID`, `dtRecordAdded`, `dtLastVisit`) VALUES (%s, %s, %s)\"\n", |
109 | 116 | " cursor.execute(sql, (nClientID, dtRecordAdded, dtLastVisit))\n", |
110 | 117 | "\n", |
111 | | - " # connection is not autocommit by default. So you must commit to save your changes.\n", |
| 118 | + " # Connection is not autocommit by default, so we must commit to save changes\n", |
112 | 119 | " connection.commit()\n", |
113 | | - " print(f'Successfully inserted record into {table_name}')\n", |
| 120 | + " print(f'Successfully inserted record into {tableName}')\n", |
114 | 121 | " \n", |
115 | 122 | " except Exception as e:\n", |
116 | 123 | " print(f'Error in insertion to MySQL database: {e}')" |
117 | 124 | ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "code", |
| 128 | + "execution_count": 6, |
| 129 | + "id": "f6894d17", |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [ |
| 132 | + { |
| 133 | + "name": "stdout", |
| 134 | + "output_type": "stream", |
| 135 | + "text": [ |
| 136 | + "Successfully inserted record into tblClients\n" |
| 137 | + ] |
| 138 | + } |
| 139 | + ], |
| 140 | + "source": [ |
| 141 | + "insert_record(tableName, nClientID, dtRecordAdded, dtLastVisit)" |
| 142 | + ] |
| 143 | + }, |
| 144 | + { |
| 145 | + "cell_type": "markdown", |
| 146 | + "id": "9a1c25f2", |
| 147 | + "metadata": {}, |
| 148 | + "source": [ |
| 149 | + "### Query table to verify insertion" |
| 150 | + ] |
| 151 | + }, |
| 152 | + { |
| 153 | + "cell_type": "code", |
| 154 | + "execution_count": 7, |
| 155 | + "id": "3c5b7bb8", |
| 156 | + "metadata": {}, |
| 157 | + "outputs": [ |
| 158 | + { |
| 159 | + "name": "stdout", |
| 160 | + "output_type": "stream", |
| 161 | + "text": [ |
| 162 | + "[{'nClientID': 'S0000001A', 'dtRecordAdded': datetime.datetime(2022, 1, 1, 0, 0), 'dtLastVisit': datetime.datetime(2022, 12, 31, 0, 0)}]\n" |
| 163 | + ] |
| 164 | + } |
| 165 | + ], |
| 166 | + "source": [ |
| 167 | + "with connection.cursor() as cursor:\n", |
| 168 | + " sql = f\"SELECT * FROM `{tableName}`\"\n", |
| 169 | + " cursor.execute(sql)\n", |
| 170 | + " result = cursor.fetchall()\n", |
| 171 | + " print(result)\n", |
| 172 | + " \n", |
| 173 | + "connection.commit()" |
| 174 | + ] |
118 | 175 | } |
119 | 176 | ], |
120 | 177 | "metadata": { |
|
0 commit comments