Skip to content

Commit e138cb7

Browse files
Update config and example notebook
1 parent b2eefa8 commit e138cb7

File tree

3 files changed

+87
-32
lines changed

3 files changed

+87
-32
lines changed

Example-Notebook.ipynb

Lines changed: 79 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"cells": [
33
{
44
"cell_type": "markdown",
5-
"id": "909edcb0",
5+
"id": "2ec6e0e7",
66
"metadata": {},
77
"source": [
8-
"## Using PyMySQL to access and modify RDS MySQL Database Instance"
8+
"# Using PyMySQL to access and modify RDS MySQL Database Instance"
99
]
1010
},
1111
{
1212
"cell_type": "markdown",
13-
"id": "f4e125bd",
13+
"id": "29d02d2b",
1414
"metadata": {},
1515
"source": [
1616
"### Necessary imports"
@@ -19,7 +19,7 @@
1919
{
2020
"cell_type": "code",
2121
"execution_count": 1,
22-
"id": "f70c9d8c",
22+
"id": "f4706480",
2323
"metadata": {},
2424
"outputs": [],
2525
"source": [
@@ -29,7 +29,7 @@
2929
},
3030
{
3131
"cell_type": "markdown",
32-
"id": "9f6288a7",
32+
"id": "5ea1c5d0",
3333
"metadata": {},
3434
"source": [
3535
"___\n",
@@ -38,8 +38,8 @@
3838
},
3939
{
4040
"cell_type": "code",
41-
"execution_count": null,
42-
"id": "caded1e3",
41+
"execution_count": 2,
42+
"id": "66a73d8e",
4343
"metadata": {},
4444
"outputs": [],
4545
"source": [
@@ -52,29 +52,36 @@
5252
" db=DBNAME,\n",
5353
" cursorclass=CURSORCLASS,\n",
5454
" ssl_ca=SSL_CA)\n",
55-
" status = 'Success'\n",
5655
" print('[+] RDS Connection Successful')\n",
5756
" 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",
6059
"\n",
61-
" return connection, status"
60+
" return connection"
6261
]
6362
},
6463
{
6564
"cell_type": "code",
66-
"execution_count": null,
67-
"id": "2e6db776",
65+
"execution_count": 3,
66+
"id": "961eb394",
6867
"metadata": {},
69-
"outputs": [],
68+
"outputs": [
69+
{
70+
"name": "stdout",
71+
"output_type": "stream",
72+
"text": [
73+
"[+] RDS Connection Successful\n"
74+
]
75+
}
76+
],
7077
"source": [
7178
"# Initiate RDS connection\n",
72-
"connection, status = start_rds_connection()"
79+
"connection = start_rds_connection()"
7380
]
7481
},
7582
{
7683
"cell_type": "markdown",
77-
"id": "42adf77b",
84+
"id": "49ffd330",
7885
"metadata": {},
7986
"source": [
8087
"___\n",
@@ -83,8 +90,8 @@
8390
},
8491
{
8592
"cell_type": "code",
86-
"execution_count": null,
87-
"id": "db24605f",
93+
"execution_count": 4,
94+
"id": "dc36379b",
8895
"metadata": {},
8996
"outputs": [],
9097
"source": [
@@ -97,8 +104,8 @@
97104
},
98105
{
99106
"cell_type": "code",
100-
"execution_count": null,
101-
"id": "361c09da",
107+
"execution_count": 5,
108+
"id": "66a455b6",
102109
"metadata": {},
103110
"outputs": [],
104111
"source": [
@@ -108,13 +115,63 @@
108115
" sql = f\"INSERT INTO `{tableName}` (`nClientID`, `dtRecordAdded`, `dtLastVisit`) VALUES (%s, %s, %s)\"\n",
109116
" cursor.execute(sql, (nClientID, dtRecordAdded, dtLastVisit))\n",
110117
"\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",
112119
" connection.commit()\n",
113-
" print(f'Successfully inserted record into {table_name}')\n",
120+
" print(f'Successfully inserted record into {tableName}')\n",
114121
" \n",
115122
" except Exception as e:\n",
116123
" print(f'Error in insertion to MySQL database: {e}')"
117124
]
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+
]
118175
}
119176
],
120177
"metadata": {

config.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import pymysql
22

3-
ACCESS_KEY_USER_KENNETH = 'xxxxx'
4-
SECRET_KEY_USER_KENNETH = 'xxxxx'
5-
USERNAME = 'admin' # Master username
6-
PASSWORD = 'xxx' # Password of RDS Credential Settings
7-
ENDPOINT = "client-database.xxxxx.ap-southeast-1.rds.amazonaws.com"
8-
PORT = 3306
9-
REGION = "ap-southeast-1"
10-
DBNAME = "client-database"
11-
CURSORCLASS = pymysql.cursors.DictCursor
12-
SSL_CA = "ssl/ap-southeast-1-bundle.pem"
3+
USERNAME = 'admin' # Replace with your master username
4+
PASSWORD = 'XXXXX' # Replace with your RDS instance password
5+
ENDPOINT = "XXXXXXX.XXXXXXX.ap-southeast-1.rds.amazonaws.com" # Replace with your RDS endpoint
6+
PORT = 3306 # Replace with the RDS instance port
7+
REGION = "ap-southeast-1b" # Replace with your AWS region
8+
DBNAME = 'schema1' # Replace with the name of your SCHEMA in MySQL workbench
9+
SSL_CA = "ssl/ap-southeast-1-bundle.pem" # Replace with the folder location of your SSL bundle
10+
CURSORCLASS = pymysql.cursors.DictCursor # No need to modify this

screenshots/10.png

42.8 KB
Loading

0 commit comments

Comments
 (0)