-
Notifications
You must be signed in to change notification settings - Fork 1
/
db_create.py
154 lines (143 loc) · 4.8 KB
/
db_create.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/python
# -*- coding: utf-8 -*-
# File name: db_create.py
'''
Script to create and set up the database
'''
import configparser
from psycopg2 import connect, ProgrammingError
import json
config = configparser.ConfigParser()
config.readfp(open('config.cfg'))
dbname = config.get('db', 'name')
user = config.get('db', 'user')
host = config.get('db', 'host')
password = config.get('db', 'password')
# create wahlomat db (deletes if already existed and creates anew)
con = connect(
dbname='postgres', user=user, host=host, password=password)
con.autocommit = True
cur = con.cursor()
try:
cur.execute('DROP DATABASE ' + dbname)
except ProgrammingError:
pass
cur.execute('CREATE DATABASE ' + dbname)
cur.close()
# connect to wahlomat db and create the necessary tables
con = connect(
dbname=dbname, user=user, host=host, password=password)
cur = con.cursor()
def create_schema():
cur.execute(
'''CREATE TABLE kategorie(
id SERIAL PRIMARY KEY,
data VARCHAR UNIQUE
)'''
)
cur.execute(
'''CREATE TABLE partei(
id SERIAL PRIMARY KEY,
data VARCHAR
)'''
)
cur.execute(
'''CREATE TABLE frage(
id SERIAL PRIMARY KEY,
data VARCHAR,
kategorie_id INTEGER REFERENCES kategorie(id)
)'''
)
cur.execute(
'''CREATE TABLE antwort(
id SERIAL PRIMARY KEY,
data VARCHAR,
wahl INTEGER,
frage_id INTEGER REFERENCES frage(id),
partei_id INTEGER REFERENCES partei(id)
)'''
)
cur.execute(
'''CREATE TABLE auswahl(
id SERIAL PRIMARY KEY,
wahl INTEGER,
frage_id INTEGER REFERENCES frage(id)
)'''
)
cur.execute(
'''CREATE TABLE statisch(
thema VARCHAR,
welcome VARCHAR,
welcometxt VARCHAR,
accenttitle VARCHAR,
resultArticle VARCHAR,
favoriteArticle VARCHAR,
neutralWarning VARCHAR,
noteResultArticle VARCHAR
)'''
)
cur.execute(
'''CREATE TABLE benutzer(
id SERIAL PRIMARY KEY,
username VARCHAR UNIQUE,
email VARCHAR UNIQUE,
pwdhash VARCHAR,
ctime TIMESTAMP default current_timestamp
)'''
)
con.commit()
def fill_data(json_file):
with open(json_file) as data_file:
jsonData = json.load(data_file)
content = jsonData['content']
questions = content['questions']
for question in questions:
for q_text in question:
cur.execute(
'''SELECT id FROM kategorie
WHERE data = (%s)
''', (question[q_text]['kategorie'], )
)
cat = cur.fetchone()
if not cat:
cur.execute(
'''INSERT INTO kategorie (data)
VALUES (%s) RETURNING id
''', (question[q_text]['kategorie'], )
)
cat = cur.fetchone()
cat_id = cat[0]
cur.execute(
'''INSERT INTO frage (data, kategorie_id)
VALUES (%s, %s) RETURNING id
''', (q_text, cat_id, )
)
q_id = cur.fetchone()[0]
for partei in question[q_text]['parteien']:
cur.execute(
'''SELECT id FROM partei
WHERE data = (%s)
''', (partei['name'], ))
party = cur.fetchone()
if not party:
cur.execute(
'''INSERT INTO partei (data)
VALUES (%s) RETURNING id
''', (partei['name'], )
)
party = cur.fetchone()
party_id = party[0]
cur.execute(
'''INSERT INTO antwort (data, wahl, frage_id, partei_id)
VALUES (%s, %s, %s, %s)
''', (partei['antwort'],
partei['wahl'], q_id, party_id, ))
# add static data
cur.execute(
'''INSERT INTO statisch (thema, welcome, welcometxt, accenttitle, resultArticle, favoriteArticle, neutralWarning, noteResultArticle)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s)
''', (content['thema'], content['welcome'], content['welcometxt'], content['accenttitle'], content['resultArticle'], content['favoriteArticle'], content['neutralWarning'], content['noteResultArticle'] ))
con.commit()
create_schema()
fill_data('data.json')
cur.close()