-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcsv_to_db.py
More file actions
90 lines (79 loc) · 2.67 KB
/
csv_to_db.py
File metadata and controls
90 lines (79 loc) · 2.67 KB
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
import csv
import mysql.connector
# Database Configuration
DB_CONFIG = {
'host': '127.0.0.1',
'user': 'foo',
'password': 'foo',
'database': 'routes_db'
}
# Function to establish a database connection
def get_db_connection():
try:
conn = mysql.connector.connect(**DB_CONFIG)
return conn
except mysql.connector.Error as err:
print(f"Error: {err}")
return None
# Function to create the database
def create_database():
conn = mysql.connector.connect(
host=DB_CONFIG['host'],
user=DB_CONFIG['user'],
password=DB_CONFIG['password']
)
cursor = conn.cursor()
cursor.execute("CREATE DATABASE IF NOT EXISTS routes_db")
conn.close()
# Function to create the table
def create_table():
conn = get_db_connection()
if conn is None:
return
cursor = conn.cursor()
cursor.execute("DROP TABLE IF EXISTS routes")
cursor.execute("""
CREATE TABLE routes (
id INT AUTO_INCREMENT PRIMARY KEY,
facility_name VARCHAR(255),
code VARCHAR(255),
country VARCHAR(255),
latitude DECIMAL(10, 8),
longitude DECIMAL(11, 8),
type VARCHAR(255),
transit_time_hours INT,
distance_km DECIMAL(10, 2),
border_crossings VARCHAR(255), -- FIXED: Changed from INT to VARCHAR(255)
currency VARCHAR(255)
)
""")
conn.commit()
cursor.close()
conn.close()
# Function to import CSV data into the database
def import_csv_to_db(csv_file):
conn = get_db_connection()
if conn is None:
return
cursor = conn.cursor()
with open(csv_file, newline='', encoding='utf-8') as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
query = """
INSERT INTO routes (facility_name, code, country, latitude, longitude, type,
transit_time_hours, distance_km, border_crossings, currency)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
cursor.execute(query, (
row['Facility Name'], row['Code'], row['Country'], row['Latitude'], row['Longitude'], row['Type'],
row['Transit Time (hrs)'], row['Distance (km)'], row['Border Crossings'], row['Currency']
))
conn.commit()
print("✅ Data imported successfully")
cursor.close()
conn.close()
# Main execution
if __name__ == "__main__":
create_database() # Ensure database exists
create_table() # Create table with correct schema
import_csv_to_db('logistics_facilities_updated.csv') # Import CSV data