-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
47 lines (42 loc) · 2.22 KB
/
models.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
import sqlite3
conn = sqlite3.connect('gmail_inbox.sqlite3')
field_map = { 'From': ['from_person', 'TEXT'], 'To': ['to_person', 'TEXT'], 'Reply-To': ['reply_to', 'TEXT'], 'Sender': ['sender', 'TEXT'], 'Subject': ['subject', 'TEXT'], 'Message-ID': ['message_id', 'TEXT UNIQUE'], 'time': ['time', 'INTEGER NOT NULL'], 'body': ['body', 'TEXT'], 'file_names': ['file_names', 'TEXT'], 'original_file_names': ['original_file_names', 'TEXT'], 'original_file_ext': ['original_file_ext', 'TEXT'], 'uid': ['uid', 'TEXT'], 'In-Reply-To': ['in_reply_to', 'TEXT'], 'References': ['email_references', 'TEXT'], 'Bcc': ['b_c_c', 'TEXT'], 'Cc': ['c_c', 'TEXT'] }
c = conn.cursor()
def create_db(table_name):
insert_string_head = 'CREATE TABLE IF NOT EXISTS ' + table_name + ' (id INTEGER PRIMARY KEY, '
field_map_values = tuple(field_map.values())
for i in range(len(field_map_values)):
key = field_map_values[i][0] + ' '
data_type = field_map_values[i][1]
type_string = data_type
if i == len(field_map_values) -1:
insert_string_head = insert_string_head + key + type_string
else:
insert_string_head = insert_string_head + key + type_string + ', '
insert_string = insert_string_head + ")"
c.execute(insert_string)
def insert_row(row, table_name):
data_tuple = tuple(row.values())
insert_string_head = 'INSERT OR IGNORE INTO ' + table_name + '('
insert_string_tail = ' VALUES('
key_tuple = tuple(row.keys())
if len(key_tuple) > 0:
for i in range(len(key_tuple)):
key = key_tuple[i]
val = data_tuple[i]
if i != len(row.keys()) -1:
insert_string_head = insert_string_head + key + ', '
insert_string_tail = insert_string_tail + '?, '
else:
insert_string_head = insert_string_head + key + ')'
insert_string_tail = insert_string_tail + '?)'
insert_string = insert_string_head + insert_string_tail
tup = tuple(row.values())
print(tup)
return c.execute(insert_string, tup)
else:
return ''
def count_rows():
amount = c.execute('SELECT COUNT (*) FROM inbox;')
rows = c.fetchall()
return rows[0]