-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaker_python.py
More file actions
77 lines (64 loc) · 2.77 KB
/
faker_python.py
File metadata and controls
77 lines (64 loc) · 2.77 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
import sqlite3
from faker import Faker
from datetime import datetime, timedelta
# Connexion à la base de données SQLite
db_connection = sqlite3.connect("identifier")
cursor = db_connection.cursor()
# Initialisation de Faker
fake = Faker()
# Générer des utilisateurs et des adresses
for _ in range(10):
firstname = fake.first_name()
lastname = fake.last_name()
username = fake.user_name()
# Insérer dans la table user
cursor.execute("INSERT INTO user (firstname, lastname, username) VALUES (?, ?, ?)", (firstname, lastname, username))
db_connection.commit()
user_id = cursor.lastrowid
street_address = fake.street_address()
city = fake.city()
postal_code = fake.postcode()
# Insérer dans la table address
cursor.execute("INSERT INTO address (user_id, street_address, city, postal_code) VALUES (?, ?, ?, ?)",
(user_id, street_address, city, postal_code))
db_connection.commit()
# Générer des produits
for _ in range(10):
product_name = fake.word()
product_description = fake.sentence()
price = fake.random_float(2, 10, 100)
stock_available = fake.random_int(1, 100)
# Insérer dans la table product
cursor.execute("INSERT INTO product (name, description, price, stock_available) VALUES (?, ?, ?, ?)",
(product_name, product_description, price, stock_available))
db_connection.commit()
# Générer des paniers et des commandes
cursor.execute("SELECT user_id FROM user")
users = cursor.fetchall()
cursor.execute("SELECT product_id FROM product")
products = cursor.fetchall()
for user in users:
cart_products = fake.random_elements(products, length=fake.random_int(1, 5))
for product in cart_products:
quantity = fake.random_int(1, 5)
cursor.execute("INSERT INTO cart (user_id, product_id, quantity) VALUES (?, ?, ?)",
(user[0], product[0], quantity))
db_connection.commit()
command_products = fake.random_elements(products, length=fake.random_int(1, 3))
for product in command_products:
quantity = fake.random_int(1, 3)
cursor.execute("INSERT INTO command (user_id, product_id, quantity) VALUES (?, ?, ?)",
(user[0], product[0], quantity))
db_connection.commit()
# Générer des factures
for user in users:
invoice_products = fake.random_elements(products, length=fake.random_int(1, 3))
for product in invoice_products:
quantity = fake.random_int(1, 3)
order_date = fake.date_time_this_decade()
cursor.execute("INSERT INTO invoices (user_id, product_id, quantity, order_date) VALUES (?, ?, ?, ?)",
(user[0], product[0], quantity, order_date))
db_connection.commit()
# Fermeture de la connexion
cursor.close()
db_connection.close()