· 6 years ago · Mar 11, 2019, 02:48 PM
1import sqlite3
2
3
4class DbHandler:
5
6 def __init__(self, dbfile="database_mah.db"):
7 self.dbfile = dbfile
8 self.connection = sqlite3.connect(dbfile)
9 self.cur = self.connection.cursor()
10
11 def setup(self):
12 statement = "CREATE TABLE IF NOT EXISTS breach (email text UNIQUE,username text,domain text,country text)"
13 self.connection.execute(statement)
14 self.connection.commit()
15
16 def get_allrecords(self):
17 statement = "SELECT * FROM breach"
18 return [x for x in self.connection.execute(statement)]
19
20 def add_items(self, emails):
21 for email in emails:
22 self._add_item(email)
23 self.connection.commit()
24
25 def _add_item(self, email):
26 statement = "INSERT OR IGNORE INTO breach (email, username, domain, country) VALUES (?, ?, ?, ?)"
27 #print(email)
28 username = email[:email.index("@")]
29 domain = email[email.index("@"):]
30 country = domain[domain.index("."):]
31 args = (email, username, domain, country)
32 self.cur.execute(statement, args)
33'''
34 def add_item1(self, email):
35 username = email[:email.index("@")]
36 domain = email[email.index("@"):]
37 country = domain[domain.index("."):]
38 self.cache.append((email, username, domain, country))
39
40 def store_items(self):
41 statement = "INSERT OR IGNORE INTO breach (email, username, domain, country) VALUES (?, ?, ?, ?)"
42 self.connection.executemany(statement, self.cache)
43 self.connection.commit()
44 self.cache = []
45'''