Skip to content

Commit c207354

Browse files
committed
Update and used a new library pymysql
1 parent 90cb8bc commit c207354

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

index.py

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,32 @@
1-
import mysql.connector
2-
# pip install mysql-connector-python <package you need to install>
3-
from mysql.connector import Error
4-
connection = mysql.connector.connect(
5-
host='database-1.c1isuua0qsuk.us-east-1.rds.amazonaws.com',
6-
database='abhinav',
7-
user='admin',
8-
password='abhi-1708'
9-
)
10-
11-
cr = connection.cursor()
12-
cr.execute("show tables")
13-
result = cr.fetchall()
14-
print(result)
1+
import pymysql.cursors
2+
# install this library pip install pymysql
3+
try:
4+
print("Attempting to connect to the database with pymysql...")
5+
connection = pymysql.connect(
6+
host='database-1.c1isuua0qsuk.us-east-1.rds.amazonaws.com',
7+
user='admin',
8+
password='abhi-1708',
9+
database='abhinav',
10+
connect_timeout=10
11+
)
12+
13+
with connection.cursor() as cursor:
14+
print("Connected to MySQL database using pymysql")
15+
cursor.execute("SHOW TABLES")
16+
result = cursor.fetchall()
17+
if result:
18+
print("Tables:", result)
19+
else:
20+
print("No tables found in the database.")
21+
22+
except pymysql.MySQLError as e:
23+
print("Error while connecting to MySQL:", e)
24+
25+
finally:
26+
if connection:
27+
connection.close()
28+
print("MySQL connection is closed")
29+
1530

1631

1732
# host: database-1.c1isuua0qsuk.us-east-1.rds.amazonaws.com <endpoint>

0 commit comments

Comments
 (0)