-
Notifications
You must be signed in to change notification settings - Fork 0
/
init_db.py
62 lines (38 loc) · 1.32 KB
/
init_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
51
52
53
54
55
56
57
58
59
60
61
62
import sqlite3
import random
from datetime import datetime, timedelta
def getrandomname():
names = ["Valentina", "Elba", "Jeroen"]
return names[random.randint(0, len(names)-1)]
def getrandomcount(length):
result = []
for i in range(length):
result.append(random.randint(0, 50))
return result
def getrandomdate(length):
result = []
now = datetime.today()
for i in range(length):
now = now - timedelta(days=1)
result.append(str(now.date()))
return result
connection = sqlite3.connect('database.db')
with open('schema.sql') as f:
connection.executescript(f.read())
cur = connection.cursor()
dates = getrandomdate(25)
count = 1
for date in dates:
postCount = random.randint(1, 10)
for pc in range(postCount):
cur.execute("INSERT INTO posts (title, content, created) VALUES (?, ?, ?)",
('Some posted text', 'Content for post', date)
)
postId = cur.lastrowid
commentCount = random.randint(1, 10)
for cc in range(commentCount):
cur.execute("INSERT INTO comments (postId, name, content, created) VALUES (?, ?, ?, ?)",
(postId, getrandomname(), 'this is the text for the comment', date)
)
connection.commit()
connection.close()