-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader.py
72 lines (59 loc) · 2.3 KB
/
loader.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
# Import required modules
import sqlite3
import csv
# Define function to create the database and table
def create_database():
# Create a connection to the database
connection = sqlite3.connect('drones.db')
# Create a cursor object to execute SQL commands
cursor = connection.cursor()
# Execute a CREATE TABLE command to create a "Drone" table with the required columns
cursor.execute("""
CREATE TABLE IF NOT EXISTS "Drone" (
"Id" INTEGER,
"Name" TEXT,
"FlightTime" INTEGER,
"SensorSize" TEXT,
"WeightMetric" NUMERIC,
"WeightImperial" NUMERIC,
"TopSpeedMetric" NUMERIC,
"TopSpeedImperial" NUMERIC,
"Cost" NUMERIC,
PRIMARY KEY("Id" AUTOINCREMENT)
);
""")
# Commit the changes to the database and close the connection
connection.commit()
connection.close()
# Define function to insert drone data into the table
def insert_drone_data(drone_data):
# Create a connection to the database
connection = sqlite3.connect('drones.db')
# Create a cursor object to execute SQL commands
cursor = connection.cursor()
# Execute an INSERT INTO command to insert the drone data into the "Drone" table
cursor.execute("""
INSERT INTO Drone (Name, FlightTime, SensorSize, WeightMetric, WeightImperial, TopSpeedMetric, TopSpeedImperial, Cost)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
""", drone_data)
# Commit the changes to the database and close the connection
connection.commit()
connection.close()
# Define function to load data from CSV file into the table
def load_csv_data(file_name):
# Open the CSV file for reading
with open(file_name, 'r') as csv_file:
# Create a CSV reader object
csv_reader = csv.reader(csv_file)
# Skip the header row
next(csv_reader)
# Loop through each row in the CSV file
for row in csv_reader:
# Convert the row to a tuple
drone_data = tuple(row)
# Insert the drone data into the table using the insert_drone_data() function
insert_drone_data(drone_data)
# If this script is run directly, call the create_database() and load_csv_data() functions
if __name__ == "__main__":
create_database()
load_csv_data('drones.csv')