File tree Expand file tree Collapse file tree 6 files changed +118
-0
lines changed
sql-queries-10/checking-database-connectivity Expand file tree Collapse file tree 6 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ MYSQL_HOST = localhost
2
+ MYSQL_PORT = 3306
3
+ MYSQL_USER = root
4
+ MYSQL_PASS = MySQL2024
5
+ MYSQL_DB = University
Original file line number Diff line number Diff line change
1
+ DB_HOST = localhost
2
+ DB_PORT = 5432
3
+ DB_USER = user
4
+ DB_PASS = Password2024
5
+ DB_NAME = University
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ # PostgreSQL Database Connectivity Check Script
4
+
5
+ # Load environment variables
6
+ source postgresql.env
7
+
8
+ echo " Testing PostgreSQL connectivity..."
9
+
10
+ # Test PostgreSQL connectivity and check for 'student' table
11
+ PGPASSWORD=" $DB_PASS " psql -h " $DB_HOST " -p " $DB_PORT " -U " $DB_USER " -d " $DB_NAME " -c ' \dt student' > /dev/null 2>&1
12
+
13
+ if [ $? -eq 0 ]; then
14
+ echo " PostgreSQL: Connection successful and student table exists."
15
+ else
16
+ echo " PostgreSQL: Connection failed or student table does not exist."
17
+ fi
Original file line number Diff line number Diff line change
1
+ #! /bin/bash
2
+
3
+ # MySQL Database Connectivity Check Script
4
+
5
+ # Load environment variables
6
+ source mysql.env
7
+
8
+ echo " Testing MySQL connectivity..."
9
+
10
+ # Test MySQL connectivity and check for 'Department' table
11
+ mysql -h " $MYSQL_HOST " -P " $MYSQL_PORT " -u " $MYSQL_USER " -p" $MYSQL_PASS " -e " USE $MYSQL_DB ; SHOW TABLES LIKE 'Department';" > /dev/null 2>&1
12
+
13
+ if [ $? -eq 0 ]; then
14
+ echo " MySQL: Connection successful and department table exists."
15
+ else
16
+ echo " MySQL: Connection failed or department table does not exist."
17
+ fi
Original file line number Diff line number Diff line change
1
+ import os
2
+ import mysql .connector
3
+ from dotenv import load_dotenv
4
+
5
+ # Load environment variables from mysql.env
6
+ load_dotenv ("mysql.env" )
7
+
8
+ MYSQL_HOST = os .getenv ("MYSQL_HOST" )
9
+ MYSQL_PORT = os .getenv ("MYSQL_PORT" )
10
+ MYSQL_USER = os .getenv ("MYSQL_USER" )
11
+ MYSQL_PASS = os .getenv ("MYSQL_PASS" )
12
+ MYSQL_DB = os .getenv ("MYSQL_DB" )
13
+
14
+ try :
15
+ connection = mysql .connector .connect (
16
+ host = MYSQL_HOST ,
17
+ port = MYSQL_PORT ,
18
+ user = MYSQL_USER ,
19
+ password = MYSQL_PASS ,
20
+ database = MYSQL_DB
21
+ )
22
+
23
+ cursor = connection .cursor ()
24
+ cursor .execute ("SHOW TABLES LIKE 'Department';" )
25
+ result = cursor .fetchone ()
26
+
27
+ if result :
28
+ print ("MySQL: Connection successful and department table exists." )
29
+ else :
30
+ print ("MySQL: Connection successful but department table does not exist." )
31
+
32
+ except mysql .connector .Error as err :
33
+ print (f"MySQL: Connection failed - { err } " )
34
+
35
+ finally :
36
+ if 'connection' in locals () and connection .is_connected ():
37
+ connection .close ()
Original file line number Diff line number Diff line change
1
+ import os
2
+ import psycopg2
3
+ from dotenv import load_dotenv
4
+
5
+ # Load environment variables from postgresql.env
6
+ load_dotenv ("postgresql.env" )
7
+
8
+ DB_HOST = os .getenv ("DB_HOST" )
9
+ DB_PORT = os .getenv ("DB_PORT" )
10
+ DB_NAME = os .getenv ("DB_NAME" )
11
+ DB_USER = os .getenv ("DB_USER" )
12
+ DB_PASS = os .getenv ("DB_PASS" )
13
+
14
+ try :
15
+ connection = psycopg2 .connect (
16
+ host = DB_HOST ,
17
+ port = DB_PORT ,
18
+ dbname = DB_NAME ,
19
+ user = DB_USER ,
20
+ password = DB_PASS
21
+ )
22
+
23
+ cursor = connection .cursor ()
24
+ cursor .execute ("SELECT to_regclass('public.student');" )
25
+ table_exists = cursor .fetchone ()[0 ]
26
+
27
+ if table_exists :
28
+ print ("PostgreSQL: Connection successful and student table exists." )
29
+ else :
30
+ print ("PostgreSQL: Connection successful but student table does not exist." )
31
+
32
+ except Exception as e :
33
+ print (f"PostgreSQL: Connection failed - { e } " )
34
+
35
+ finally :
36
+ if 'connection' in locals ():
37
+ connection .close ()
You can’t perform that action at this time.
0 commit comments