-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_table.py
60 lines (48 loc) · 2.19 KB
/
create_table.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
from datetime import datetime, timedelta
import random
class CreateTable:
def __init__(self, conn1, conn2):
"""
Initialize the HashJoin object.
:param conn1: Connection to the first database
:param conn2: Connection to the second database
"""
self.conn1 = conn1
self.conn2 = conn2
self.cursor1 = conn1.cursor()
self.cursor2 = conn2.cursor()
@staticmethod
def generate_timestamp(primary_key, seed):
random.seed(primary_key + seed) # Incorporate primary_key into the seed
start_date = datetime(2023, 6, 1)
random_date = start_date + timedelta(days=primary_key)
random_time = timedelta(
hours=random.randint(0, 23),
minutes=random.randint(0, 59),
seconds=random.randint(0, 59)
)
timestamp = random_date + random_time
return timestamp.strftime("%Y-%m-%d %H:%M:%S")
def create_table(self, connection, cursor, table_name, num_records, seed=None):
# Delete the table if it exists
cursor.execute(f'DROP TABLE IF EXISTS {table_name}')
# Create a table if it doesn't exist
cursor.execute(f'''CREATE TABLE {table_name}
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
email TEXT,
timestamp TEXT)''')
# Insert random data into the table with random timestamps
for i in range(1, num_records):
name = f"Name_{random.randint(1, 30000000)}"
email = f"email_{random.randint(1, 30000000)}@example.com"
timestamp = self.generate_timestamp(i, seed)
cursor.execute(f'INSERT INTO {table_name} (name, email, timestamp) VALUES (?, ?, ?)',
(name, email, timestamp))
# Commit the changes
connection.commit()
def create_tables(self, size1, size2, seed=None):
# Create table1
self.create_table(self.conn1, self.cursor1, 'table1', size1, seed)
# Create table2
self.create_table(self.conn2, self.cursor2, 'table2', size2, seed+100)