Skip to content

Commit b88f031

Browse files
committed
Minor changes made
1 parent a40d5c2 commit b88f031

File tree

6 files changed

+53
-114
lines changed

6 files changed

+53
-114
lines changed

sql-queries-10/checking-database-connectivity/mysql-check.sh

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,13 @@
22

33
# MySQL Database Connectivity Check Script
44

5-
# Create .env file if it doesn't exist
6-
if [ ! -f .env ]; then
7-
cat > .env <<EOF
8-
# MySQL Configuration
9-
MYSQL_HOST="localhost"
10-
MYSQL_PORT="3306"
11-
MYSQL_USER="root"
12-
MYSQL_PASS="MySQL!2024"
13-
MYSQL_DB="University"
14-
EOF
15-
echo "Created .env file with MySQL configuration"
16-
fi
17-
185
# Load environment variables
19-
source .env
6+
source mysql.env
207

218
echo "Testing MySQL connectivity..."
229

2310
# Test MySQL connectivity and check for 'Department' table
24-
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
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
2512

2613
if [ $? -eq 0 ]; then
2714
echo "MySQL: Connection successful and department table exists."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MYSQL_HOST="localhost"
2+
MYSQL_PORT="3306"
3+
MYSQL_USER="root"
4+
MYSQL_PASS="MySQL!2024"
5+
MYSQL_DB="University"

sql-queries-10/checking-database-connectivity/postgres-check.sh

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@
22

33
# PostgreSQL Database Connectivity Check Script
44

5-
# Create .env file if it doesn't exist
6-
if [ ! -f .env ]; then
7-
cat > .env <<EOF
8-
DB_HOST="localhost"
9-
DB_PORT="5432"
10-
DB_USER="user"
11-
DB_PASS="Password!2024"
12-
DB_NAME="University"
13-
EOF
14-
echo "Created .env file with PostgreSQL configuration"
15-
fi
16-
175
# Load environment variables
186
source .env
197

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DB_HOST="localhost"
2+
DB_PORT="5432"
3+
DB_USER="user"
4+
DB_PASS="Password!2024"
5+
DB_NAME="University"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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()
38+
EOF
Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
1-
#!/bin/bash
2-
3-
# Python Database Connectivity Check Script
4-
5-
# Check if Python is installed
6-
if ! command -v python3 &> /dev/null; then
7-
echo "Python 3 is not installed. Please install Python 3 first."
8-
exit 1
9-
fi
10-
11-
# Create .env file if it doesn't exist
12-
if [ ! -f .env ]; then
13-
cat > .env <<EOF
14-
# PostgreSQL Configuration
15-
DB_HOST="localhost"
16-
DB_PORT="5432"
17-
DB_USER="user"
18-
DB_PASS="Password!2024"
19-
DB_NAME="University"
20-
21-
# MySQL Configuration
22-
MYSQL_HOST="localhost"
23-
MYSQL_PORT="3306"
24-
MYSQL_USER="root"
25-
MYSQL_PASS="MySQL!2024"
26-
MYSQL_DB="University"
27-
EOF
28-
echo "Created .env file with database configurations"
29-
fi
30-
31-
# Install required Python packages
32-
echo "Installing required Python packages..."
33-
pip install psycopg2-binary mysql-connector-python python-dotenv > /dev/null 2>&1
34-
35-
# Create PostgreSQL test script
36-
cat > test_pg_connect.py <<EOF
371
import os
382
import psycopg2
393
from dotenv import load_dotenv
404

41-
# Load environment variables from .env
42-
load_dotenv()
5+
# Load environment variables from postgresql.env
6+
load_dotenv("postgresql.env")
437

448
DB_HOST = os.getenv("DB_HOST")
459
DB_PORT = os.getenv("DB_PORT")
@@ -71,52 +35,4 @@
7135
finally:
7236
if 'connection' in locals():
7337
connection.close()
74-
EOF
75-
76-
# Create MySQL test script
77-
cat > test_mysql_connect.py <<EOF
78-
import os
79-
import mysql.connector
80-
from dotenv import load_dotenv
81-
82-
# Load environment variables from .env
83-
load_dotenv()
84-
85-
MYSQL_HOST = os.getenv("MYSQL_HOST")
86-
MYSQL_PORT = os.getenv("MYSQL_PORT")
87-
MYSQL_USER = os.getenv("MYSQL_USER")
88-
MYSQL_PASS = os.getenv("MYSQL_PASS")
89-
MYSQL_DB = os.getenv("MYSQL_DB")
90-
91-
try:
92-
connection = mysql.connector.connect(
93-
host=MYSQL_HOST,
94-
port=MYSQL_PORT,
95-
user=MYSQL_USER,
96-
password=MYSQL_PASS,
97-
database=MYSQL_DB
98-
)
99-
100-
cursor = connection.cursor()
101-
cursor.execute("SHOW TABLES LIKE 'Department';")
102-
result = cursor.fetchone()
103-
104-
if result:
105-
print("MySQL: Connection successful and department table exists.")
106-
else:
107-
print("MySQL: Connection successful but department table does not exist.")
108-
109-
except mysql.connector.Error as err:
110-
print(f"MySQL: Connection failed - {err}")
111-
112-
finally:
113-
if 'connection' in locals() and connection.is_connected():
114-
connection.close()
115-
EOF
116-
117-
# Run the Python tests
118-
echo "Running PostgreSQL test..."
119-
python3 test_pg_connect.py
120-
121-
echo "Running MySQL test..."
122-
python3 test_mysql_connect.py
38+
EOF

0 commit comments

Comments
 (0)