diff --git a/sql-queries-10/checking-database-connectivity/mysql.env b/sql-queries-10/checking-database-connectivity/mysql.env new file mode 100644 index 00000000..3123e75c --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/mysql.env @@ -0,0 +1,5 @@ +MYSQL_HOST=localhost +MYSQL_PORT=3306 +MYSQL_USER=root +MYSQL_PASS=MySQL2024 +MYSQL_DB=University \ No newline at end of file diff --git a/sql-queries-10/checking-database-connectivity/postgresql.env b/sql-queries-10/checking-database-connectivity/postgresql.env new file mode 100644 index 00000000..c26b9d24 --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/postgresql.env @@ -0,0 +1,5 @@ +DB_HOST=localhost +DB_PORT=5432 +DB_USER=user +DB_PASS=Password2024 +DB_NAME=University \ No newline at end of file diff --git a/sql-queries-10/checking-database-connectivity/test-db-connect.sh b/sql-queries-10/checking-database-connectivity/test-db-connect.sh new file mode 100644 index 00000000..bb7b9f09 --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/test-db-connect.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# PostgreSQL Database Connectivity Check Script + +# Load environment variables +source postgresql.env + +echo "Testing PostgreSQL connectivity..." + +# Test PostgreSQL connectivity and check for 'student' table +PGPASSWORD="$DB_PASS" psql -h "$DB_HOST" -p "$DB_PORT" -U "$DB_USER" -d "$DB_NAME" -c '\dt student' >/dev/null 2>&1 + +if [ $? -eq 0 ]; then + echo "PostgreSQL: Connection successful and student table exists." +else + echo "PostgreSQL: Connection failed or student table does not exist." +fi diff --git a/sql-queries-10/checking-database-connectivity/test-mysql-connect.sh b/sql-queries-10/checking-database-connectivity/test-mysql-connect.sh new file mode 100644 index 00000000..7eee8a44 --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/test-mysql-connect.sh @@ -0,0 +1,17 @@ +#!/bin/bash + +# MySQL Database Connectivity Check Script + +# Load environment variables +source mysql.env + +echo "Testing MySQL connectivity..." + +# Test MySQL connectivity and check for 'Department' table +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 + +if [ $? -eq 0 ]; then + echo "MySQL: Connection successful and department table exists." +else + echo "MySQL: Connection failed or department table does not exist." +fi diff --git a/sql-queries-10/checking-database-connectivity/test_mysql_connect.py b/sql-queries-10/checking-database-connectivity/test_mysql_connect.py new file mode 100644 index 00000000..8ed0a4eb --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/test_mysql_connect.py @@ -0,0 +1,37 @@ +import os +import mysql.connector +from dotenv import load_dotenv + +# Load environment variables from mysql.env +load_dotenv("mysql.env") + +MYSQL_HOST = os.getenv("MYSQL_HOST") +MYSQL_PORT = os.getenv("MYSQL_PORT") +MYSQL_USER = os.getenv("MYSQL_USER") +MYSQL_PASS = os.getenv("MYSQL_PASS") +MYSQL_DB = os.getenv("MYSQL_DB") + +try: + connection = mysql.connector.connect( + host=MYSQL_HOST, + port=MYSQL_PORT, + user=MYSQL_USER, + password=MYSQL_PASS, + database=MYSQL_DB + ) + + cursor = connection.cursor() + cursor.execute("SHOW TABLES LIKE 'Department';") + result = cursor.fetchone() + + if result: + print("MySQL: Connection successful and department table exists.") + else: + print("MySQL: Connection successful but department table does not exist.") + +except mysql.connector.Error as err: + print(f"MySQL: Connection failed - {err}") + +finally: + if 'connection' in locals() and connection.is_connected(): + connection.close() \ No newline at end of file diff --git a/sql-queries-10/checking-database-connectivity/test_pg_connect.py b/sql-queries-10/checking-database-connectivity/test_pg_connect.py new file mode 100644 index 00000000..2d6761b3 --- /dev/null +++ b/sql-queries-10/checking-database-connectivity/test_pg_connect.py @@ -0,0 +1,37 @@ +import os +import psycopg2 +from dotenv import load_dotenv + +# Load environment variables from postgresql.env +load_dotenv("postgresql.env") + +DB_HOST = os.getenv("DB_HOST") +DB_PORT = os.getenv("DB_PORT") +DB_NAME = os.getenv("DB_NAME") +DB_USER = os.getenv("DB_USER") +DB_PASS = os.getenv("DB_PASS") + +try: + connection = psycopg2.connect( + host=DB_HOST, + port=DB_PORT, + dbname=DB_NAME, + user=DB_USER, + password=DB_PASS + ) + + cursor = connection.cursor() + cursor.execute("SELECT to_regclass('public.student');") + table_exists = cursor.fetchone()[0] + + if table_exists: + print("PostgreSQL: Connection successful and student table exists.") + else: + print("PostgreSQL: Connection successful but student table does not exist.") + +except Exception as e: + print(f"PostgreSQL: Connection failed - {e}") + +finally: + if 'connection' in locals(): + connection.close() \ No newline at end of file