· 6 years ago · Mar 12, 2019, 11:10 AM
1import sqlite3
2
3
4class DbHandler:
5
6 def __init__(self, dbfile="database_CM_method_2.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 PRIMARY KEY,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 try:
23 self._add_item(email)
24 except sqlite3.IntegrityError:
25 print("duplicated", email, "skipped.")
26 self.connection.commit()
27
28 def _add_item(self, email):
29 statement = "INSERT OR IGNORE INTO breach (email, username, domain, country) VALUES (?, ?, ?, ?)"
30 #print(email)
31 username = email[:email.index("@")]
32 domain = email[email.index("@"):]
33 country = domain[domain.index("."):]
34 args = (email, username, domain, country)
35 self.cur.execute(statement, args)
36'''
37 def add_item1(self, email):
38 username = email[:email.index("@")]
39 domain = email[email.index("@"):]
40 country = domain[domain.index("."):]
41 self.cache.append((email, username, domain, country))
42
43 def store_items(self):
44 statement = "INSERT OR IGNORE INTO breach (email, username, domain, country) VALUES (?, ?, ?, ?)"
45 self.connection.executemany(statement, self.cache)
46 self.connection.commit()
47 self.cache = []
48'''