-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.py
51 lines (44 loc) · 1.49 KB
/
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
import sys
import sqlite3 as sql
def makeGraveDatabase():
conn = sql.connect('graves.db')
c = conn.cursor()
c.execute('''CREATE TABLE findAGrave
(graveid INTEGER PRIMARY KEY, url TEXT,
name TEXT, birth TEXT, birthplace TEXT, death TEXT, deathplace TEXT,
burial TEXT, plot TEXT, more_info BOOL)''')
conn.close()
def addRowToDatabase(grave):
row = (grave['id'],)
keys = ['graveid']
for key in grave.keys():
if key == 'id':
continue
row += (grave[key],)
keys.append(key)
col_names = '(' + ', '.join(keys) + ')'
value_hold = '(' + '?,' * (len(keys) - 1) + '?)'
insert = 'INSERT INTO findAGrave ' + col_names + ' VALUES ' + value_hold
try:
conn = sql.connect('graves.db')
c = conn.cursor()
c.executemany(insert, [row])
conn.commit()
conn.close()
except:
print('Memorial #' + grave['id'] + ' is already in database.')
def extractBirth(grave, str):
try:
if 'Birthplace' in str:
grave.update({'birth': str.split('\n')[0]})
grave.update({'birthplace': str.split('\n')[1].replace('Birthplace: ', '')})
else:
grave.update({'birth': str})
except:
print('error:', sys.exc_info())
def extractDeath(grave, str):
if 'Death place:' in str:
grave['death'] = str.split('\n')[0]
grave['deathplace'] = str.split('\n')[1].replace('Death place: ', '')
else:
grave['death'] = str