-
Notifications
You must be signed in to change notification settings - Fork 0
/
initdb.py
executable file
·93 lines (82 loc) · 2.62 KB
/
initdb.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/local/bin/python3
import mariadb
import sys
from config import config
try:
conn = mariadb.connect(
host=config["db_host"],
database=config["db_name"],
port=int(config["db_port"]),
user=config["db_user"],
password=config["db_password"],
)
except mariadb.Error as e:
print(f"Error connecting to MariaDB {e}")
sys.exit(1)
cur = conn.cursor()
queries = [
"DROP DATABASE kindle",
"CREATE OR REPLACE DATABASE kindle",
# Tables
"""
CREATE TABLE kindle.Book (
BookId int NOT NULL AUTO_INCREMENT,
Name varchar(255),
AuthorId int,
PRIMARY KEY(BookId)
)
""",
"""
CREATE TABLE kindle.Author (
AuthorId int NOT NULL AUTO_INCREMENT,
Name varchar(255),
PRIMARY KEY(AuthorId))
""",
"""
CREATE TABLE kindle.Quote (
QuoteId int NOT NULL AUTO_INCREMENT,
Text varchar(2000),
DateAdded date,
Page int,
Location varchar(255),
BookId int,
AuthorId int,
PRIMARY KEY (QuoteId))
""",
"""
CREATE TABLE kindle.Temporary (
QuoteId int NOT NULL AUTO_INCREMENT,
Text varchar(2000),
DateAdded date,
Page int,
Location varchar(255),
BookId int,
AuthorId int,
PRIMARY KEY (QuoteId))
""",
# Foreign keys
"ALTER TABLE kindle.Book ADD CONSTRAINT Author_FK FOREIGN KEY (AuthorId) REFERENCES kindle.Author(AuthorId)",
"ALTER TABLE kindle.Quote ADD CONSTRAINT Quote_Book_FK FOREIGN KEY (BookId) REFERENCES kindle.Book(BookId)",
"ALTER TABLE kindle.Quote ADD CONSTRAINT Quote_Author_FK FOREIGN KEY (AuthorId) REFERENCES kindle.Author(AuthorId)",
"ALTER TABLE kindle.Temporary ADD CONSTRAINT Temporary_Book_FK FOREIGN KEY (BookId) REFERENCES kindle.Book(BookId)",
"ALTER TABLE kindle.Temporary ADD CONSTRAINT Temporary_Author_FK FOREIGN KEY (AuthorId) REFERENCES kindle.Author(AuthorId)",
]
for query in queries:
cur.execute(query)
authors = [(1, "Guy Patterson")]
cur.executemany("INSERT INTO kindle.Author VALUES (?, ?)", authors)
books = [(1, "The Wonders", 1)]
cur.executemany("INSERT INTO kindle.Book VALUES (?, ?, ?)", books)
cur.execute(
"insert into kindle.Temporary values (1, 'I am Spartacus', '2021-05-03', 5, 'Loc 2-3', 1, 1)"
)
cur.execute(
"insert into kindle.Quote values (1, 'I am Spartacus', '2021-05-03', 5, 'Loc 2-3', 1, 1)"
)
conn.commit()
cur.execute(
"""
SELECT q.QuoteId, a.Name, b.Name FROM kindle.Quote q, kindle.Author a, kindle.Book b WHERE q.BookId = b.BookId and b.AuthorId = a.AuthorId
"""
)
print([row for row in cur])