Skip to content

Commit b51ce83

Browse files
Initial commit
1 parent 3e7e045 commit b51ce83

File tree

17 files changed

+277
-0
lines changed

17 files changed

+277
-0
lines changed

Example-Notebook.ipynb

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "909edcb0",
6+
"metadata": {},
7+
"source": [
8+
"## Using PyMySQL to access and modify RDS MySQL Database Instance"
9+
]
10+
},
11+
{
12+
"cell_type": "markdown",
13+
"id": "f4e125bd",
14+
"metadata": {},
15+
"source": [
16+
"### Necessary imports"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 1,
22+
"id": "f70c9d8c",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import pymysql.cursors\n",
27+
"from config import * # Import parameters for PyMySQL connection e.g. ENDPOINT, PORT etc."
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"id": "9f6288a7",
33+
"metadata": {},
34+
"source": [
35+
"___\n",
36+
"### Create RDS connection"
37+
]
38+
},
39+
{
40+
"cell_type": "code",
41+
"execution_count": null,
42+
"id": "caded1e3",
43+
"metadata": {},
44+
"outputs": [],
45+
"source": [
46+
"def start_rds_connection():\n",
47+
" try:\n",
48+
" connection = pymysql.connect(host=ENDPOINT,\n",
49+
" port=PORT,\n",
50+
" user=USERNAME,\n",
51+
" passwd=PASSWORD,\n",
52+
" db=DBNAME,\n",
53+
" cursorclass=CURSORCLASS,\n",
54+
" ssl_ca=SSL_CA)\n",
55+
" status = 'Success'\n",
56+
" print('[+] RDS Connection Successful')\n",
57+
" except Exception as e:\n",
58+
" print(f'[+] RDS Connection Failed: {e}')\n",
59+
" status, connection = 'Failed', None\n",
60+
"\n",
61+
" return connection, status"
62+
]
63+
},
64+
{
65+
"cell_type": "code",
66+
"execution_count": null,
67+
"id": "2e6db776",
68+
"metadata": {},
69+
"outputs": [],
70+
"source": [
71+
"# Initiate RDS connection\n",
72+
"connection, status = start_rds_connection()"
73+
]
74+
},
75+
{
76+
"cell_type": "markdown",
77+
"id": "42adf77b",
78+
"metadata": {},
79+
"source": [
80+
"___\n",
81+
"### Run CRUD Operations (e.g. INSERT)"
82+
]
83+
},
84+
{
85+
"cell_type": "code",
86+
"execution_count": null,
87+
"id": "db24605f",
88+
"metadata": {},
89+
"outputs": [],
90+
"source": [
91+
"# Generate dummy Python variables as demo record for insertion into database\n",
92+
"tableName = 'tblClients'\n",
93+
"nClientID = 'S0000001A'\n",
94+
"dtRecordAdded = '2022-01-01'\n",
95+
"dtLastVisit = '2022-12-31'"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"id": "361c09da",
102+
"metadata": {},
103+
"outputs": [],
104+
"source": [
105+
"def insert_record(tableName, nClientID, dtRecordAdded, dtLastVisit):\n",
106+
" try:\n",
107+
" with connection.cursor() as cursor:\n",
108+
" sql = f\"INSERT INTO `{tableName}` (`nClientID`, `dtRecordAdded`, `dtLastVisit`) VALUES (%s, %s, %s)\"\n",
109+
" cursor.execute(sql, (nClientID, dtRecordAdded, dtLastVisit))\n",
110+
"\n",
111+
" # connection is not autocommit by default. So you must commit to save your changes.\n",
112+
" connection.commit()\n",
113+
" print(f'Successfully inserted record into {table_name}')\n",
114+
" \n",
115+
" except Exception as e:\n",
116+
" print(f'Error in insertion to MySQL database: {e}')"
117+
]
118+
}
119+
],
120+
"metadata": {
121+
"kernelspec": {
122+
"display_name": "env_foodking",
123+
"language": "python",
124+
"name": "env_foodking"
125+
},
126+
"language_info": {
127+
"codemirror_mode": {
128+
"name": "ipython",
129+
"version": 3
130+
},
131+
"file_extension": ".py",
132+
"mimetype": "text/x-python",
133+
"name": "python",
134+
"nbconvert_exporter": "python",
135+
"pygments_lexer": "ipython3",
136+
"version": "3.9.7"
137+
}
138+
},
139+
"nbformat": 4,
140+
"nbformat_minor": 5
141+
}

config.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pymysql
2+
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"

screenshots/1.png

159 KB
Loading

screenshots/11.png

44 KB
Loading

screenshots/12.png

20 KB
Loading

screenshots/13.png

99.5 KB
Loading

screenshots/14.png

564 KB
Loading

screenshots/1b.png

100 KB
Loading

screenshots/2.png

222 KB
Loading

screenshots/3.png

164 KB
Loading

0 commit comments

Comments
 (0)