-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinsert_data_to_wib_db.py
50 lines (33 loc) · 1.31 KB
/
insert_data_to_wib_db.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
import psycopg2
from random import randint
from faker import Faker #pip install faker
from random import choice
fake = Faker()
#print(fake.date_between(start_date='-3y', end_date='today'))
conn = psycopg2.connect(
host="localhost",
database="wib",
user="user_",
password="abc123")
cursor = conn.cursor()
def insert_db_data1(n):
for i in range(n):
cursor.execute(f'INSERT INTO public."Users"(age) VALUES ({randint(14, 80)};')
cursor.execute(f'INSERT INTO public."Items"(price) VALUES ({randint(100, 100000)});')
conn.commit()
insert_db_data1(200)
# print('------')
sel = cursor.execute('select "userId" from "public"."Users"')
usersIds = [i[0] for i in cursor.fetchall()]
# print('------')
sel = cursor.execute('select "itemId" from "public"."Items"')
itemsIds = [i[0] for i in cursor.fetchall()]
def insert_db_data2(n):
for i in range(n):
cursor.execute(f"""INSERT INTO public."Purchases"("userId", "itemId", "date")
VALUES ({choice(usersIds)}, {choice(itemsIds)},
'{fake.date_between(start_date="-3y", end_date="today").strftime('%Y-%m-%d')}'); """)
conn.commit()
insert_db_data2(1000)
sel = cursor.execute('select * from public."Purchases"')
print(cursor.fetchall())