-
Notifications
You must be signed in to change notification settings - Fork 0
/
Car.py
54 lines (45 loc) · 1.85 KB
/
Car.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
import mysql.connector
from faker import Faker
import random
def create_car(db):
cursor = db.cursor()
fake = Faker()
cursor.execute("""
CREATE TABLE IF NOT EXISTS Car (
CarID INT AUTO_INCREMENT PRIMARY KEY,
VariantID INT,
CategoryID INT,
EngineID INT,
ColorID INT,
ModelID INT,
VIN VARCHAR(17) UNIQUE,
Mileage FLOAT,
YearOfManufacture INT,
BrandCompany VARCHAR(100),
FOREIGN KEY (VariantID) REFERENCES CarVariant(VariantID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (CategoryID) REFERENCES CarCategory(CategoryID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (EngineID) REFERENCES CarEngine(EngineID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (ColorID) REFERENCES CarColor(ColorID)
ON UPDATE CASCADE
ON DELETE CASCADE,
FOREIGN KEY (ModelID) REFERENCES CarModel(ModelID)
ON UPDATE CASCADE
ON DELETE CASCADE
)
""")
car_data = []
for _ in range(100):
car_data.append((None, random.randint(1, 8), random.randint(1, 5), random.randint(1, 4),
random.randint(1, 10), random.randint(1, 15), fake.unique.random_int(min=1000000000000000, max=9999999999999999),
round(random.uniform(12.0, 25.0), 2), random.randint(2014, 2023), "Maruti Suzuki"))
insert_query = "INSERT INTO Car (CarID, VariantID, CategoryID, EngineID, ColorID, ModelID, VIN, Mileage, YearOfManufacture, BrandCompany) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.executemany(insert_query, car_data)
db.commit()
cursor.close()
print("Car table created and populated successfully.")