· 6 years ago · Mar 19, 2019, 07:02 PM
1import sqlite3
2
3
4class DbHandler:
5
6 def __init__(self, dbfile="database_using_pragma81.db"):
7 self.dbfile = dbfile
8 self.connection = sqlite3.connect(dbfile)
9 self.cur = self.connection.cursor()
10 self.connection.execute('pragma journal_mode=MEMORY')
11 self.connection.execute('pragma SYNCHRONOUS=1')
12 self.connection.execute('pragma PAGE_SIZE=4096')
13 self.connection.execute('pragma cache_size = 8192')
14 self.connection.execute('pragma auto_vacuum=1')
15 self.cache = []
16
17 def setup(self):
18 statement = "CREATE TABLE IF NOT EXISTS breach (username text, id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)"
19 self.connection.execute(statement)
20 self.connection.commit()
21
22 def add_item(self, email):
23 username = email[:email.index("@")]
24 #domain = email[email.index("@")+1:]
25 #country = domain[domain.index("."):]
26 self.cache.append((username,)) # must keep the comma because it's a tuple
27
28 def store_items(self):
29 statement = "INSERT OR IGNORE INTO breach (username) VALUES (?)"
30 self.connection.executemany(statement, self.cache)
31 self.connection.commit()
32 self.cache = []