first_project
This commit is contained in:
26
test_mysql_connection.py
Normal file
26
test_mysql_connection.py
Normal file
@@ -0,0 +1,26 @@
|
||||
|
||||
import mysql.connector
|
||||
|
||||
def get_connection_details():
|
||||
details = {}
|
||||
with open('setting.txt', 'r') as f:
|
||||
for line in f:
|
||||
key, value = line.strip().split(':', 1)
|
||||
details[key.strip()] = value.strip()
|
||||
return details
|
||||
|
||||
try:
|
||||
config = get_connection_details()
|
||||
conn = mysql.connector.connect(
|
||||
host=config.get('Host'),
|
||||
port=config.get('Port'),
|
||||
user=config.get('Username'),
|
||||
password=config.get('Password'),
|
||||
database=config.get('Database')
|
||||
)
|
||||
print("Connection successful!")
|
||||
conn.close()
|
||||
except ImportError:
|
||||
print("Error: mysql-connector-python is not installed. Please install it using: pip install mysql-connector-python")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
Reference in New Issue
Block a user