File tree 1 file changed +29
-14
lines changed
1 file changed +29
-14
lines changed Original file line number Diff line number Diff line change 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
+
15
30
16
31
17
32
# host: database-1.c1isuua0qsuk.us-east-1.rds.amazonaws.com <endpoint>
You can’t perform that action at this time.
0 commit comments