-
Notifications
You must be signed in to change notification settings - Fork 272
/
Copy pathDB_Automation.py
70 lines (59 loc) · 2.25 KB
/
DB_Automation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import mysql.connector
# Establish the MySQL database connection
def connect_to_mysql(host, user, password, database):
try:
connection = mysql.connector.connect(
host=host,
user=user,
password=password,
database=database
)
print("Connection established successfully.")
return connection
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
# Function to fetch data from a table
def fetch_data_from_table(connection, query):
cursor = connection.cursor(dictionary=True) # Using dictionary cursor for easier access to columns by name
try:
cursor.execute(query)
result = cursor.fetchall() # Fetch all rows
return result
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
finally:
cursor.close()
# Function to sort the data (by a specific column)
def sort_data(data, sort_by_column):
# Sort the data based on the column name (e.g., 'age')
return sorted(data, key=lambda x: x[sort_by_column])
# Main function to run the process
def main():
# MySQL connection parameters
host = 'localhost' # Change this to your MySQL host
user = 'root' # Change this to your MySQL username
password = 'yourpassword' # Change this to your MySQL password
database = 'yourdatabase' # Change this to your MySQL database name
# Connect to MySQL database
connection = connect_to_mysql(host, user, password, database)
if connection:
# Example query to get data from a table (change 'your_table' to your actual table name)
query = "SELECT * FROM your_table"
# Fetch the data
data = fetch_data_from_table(connection, query)
if data:
print("Original Data:")
for row in data:
print(row)
# Sort the data by a specific column, e.g., 'age' or 'name'
sorted_data = sort_data(data, 'your_column_to_sort_by') # Replace with actual column name
print("\nSorted Data:")
for row in sorted_data:
print(row)
# Close the connection
connection.close()
# Run the main function
if __name__ == "__main__":
main()