-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_connection.py
135 lines (112 loc) · 5.31 KB
/
db_connection.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import mysql.connector
import pandas as pd
import os
import subprocess
def connect_to_db():
print("Connecting to MySQL database...")
connection = mysql.connector.connect(
host="localhost",
user="root",
password="ArxesGlwsswn1!",
database="hotel_booking"
)
print("Connected to MySQL database!")
return connection
def convert_nan_to_none(df):
return df.applymap(lambda x: None if pd.isna(x) else x)
def insert_booking_data(df):
df = convert_nan_to_none(df)
connection = connect_to_db()
cursor = connection.cursor()
for index, row in df.iterrows():
sql = """
INSERT INTO bookings (
hotel, is_canceled, lead_time, arrival_date_year, arrival_date_month,
arrival_date_week_number, arrival_date_day_of_month, stays_in_weekend_nights,
stays_in_week_nights, adults, children, babies, meal, country,
market_segment, distribution_channel, is_repeated_guest,
previous_cancellations, previous_bookings_not_canceled, reserved_room_type,
assigned_room_type, booking_changes, deposit_type, agent, company,
days_in_waiting_list, customer_type, adr, required_car_parking_spaces,
total_of_special_requests, reservation_status, reservation_status_date,
name, email, phone_number, credit_card
) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
values = (
row['hotel'], row['is_canceled'], row['lead_time'], row['arrival_date_year'], row['arrival_date_month'],
row['arrival_date_week_number'], row['arrival_date_day_of_month'], row['stays_in_weekend_nights'],
row['stays_in_week_nights'], row['adults'],
None if pd.isna(row['children']) else row['children'],
row['babies'], row['meal'], row['country'],
row['market_segment'], row['distribution_channel'], row['is_repeated_guest'],
row['previous_cancellations'], row['previous_bookings_not_canceled'], row['reserved_room_type'],
row['assigned_room_type'], row['booking_changes'], row['deposit_type'],
None if pd.isna(row['agent']) else row['agent'],
None if pd.isna(row['company']) else row['company'],
row['days_in_waiting_list'], row['customer_type'], row['adr'], row['required_car_parking_spaces'],
row['total_of_special_requests'], row['reservation_status'], row['reservation_status_date'],
row['name'], row['email'], row.get('phone_number', None), row['credit_card']
)
# Debug
print(f"Row index: {index}")
print(f"SQL query: {sql}")
print(f"Values: {values}")
print(f"Number of placeholders in SQL: {sql.count('%s')}")
print(f"Number of values provided: {len(values)}")
cursor.execute(sql, values)
connection.commit()
cursor.close()
connection.close()
def retrieve_booking_data():
connection = connect_to_db()
query = """
SELECT
hotel, is_canceled, lead_time, arrival_date_year, arrival_date_month,
arrival_date_week_number, arrival_date_day_of_month, stays_in_weekend_nights,
stays_in_week_nights, adults, children, babies, meal, country,
market_segment, distribution_channel, is_repeated_guest,
previous_cancellations, previous_bookings_not_canceled, reserved_room_type,
assigned_room_type, booking_changes, deposit_type, agent, company,
days_in_waiting_list, customer_type, adr, required_car_parking_spaces,
total_of_special_requests, reservation_status, reservation_status_date,
name, email, phone_number, credit_card
FROM bookings
"""
df = pd.read_sql(query, connection)
connection.close()
return df
def save_tables_to_csv(table_names, connection_params):
"""
Save specified tables from a MySQL database to CSV files.
Parameters:
- table_names (list of str): List of table names to be saved.
- connection_params (dict): Dictionary with connection parameters.
"""
os.makedirs('tables', exist_ok=True)
connection = mysql.connector.connect(**connection_params)
for table in table_names:
query = f"SELECT * FROM {table}"
df = pd.read_sql(query, connection)
csv_path = os.path.join('tables', f"{table}.csv")
df.to_csv(csv_path, index=False)
connection.close()
def export_db_schema(connection_params):
os.makedirs('tables', exist_ok=True)
dump_command = (
f"mysqldump --user={connection_params['user']} "
f"--password={connection_params['password']} "
f"--host={connection_params['host']} "
f"--no-data {connection_params['database']} > tables/schema.sql"
)
subprocess.run(dump_command, shell=True, check=True)
print("Database schema has been exported to 'tables/schema.sql'")
if __name__ == "__main__":
connection_params = {
'host': 'localhost',
'user': 'root',
'password': 'ArxesGlwsswn1!',
'database': 'hotel_booking'
}
table_names = ['basic_statistics', 'booking_distribution']
save_tables_to_csv(table_names, connection_params)
export_db_schema(connection_params)