-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.py
34 lines (26 loc) · 1.44 KB
/
Database.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
import sqlite3, logging, random
logger = logging.getLogger(__name__)
class Database:
def __init__(self, database_dir, streamer):
self.database_dir = database_dir
# Remove the # which may be present in the name
self.streamer = streamer.lower().replace("#", "")
# Add the streamer with 0 deaths initially, unless it already exists.
logger.debug("Creating Database...")
self.execute('CREATE TABLE IF NOT EXISTS DeathCounter (streamer varchar(30), counter INTEGER, PRIMARY KEY (streamer));')
logger.debug("Database created.")
self.execute('INSERT OR IGNORE INTO DeathCounter(streamer, counter) VALUES (?, 0)', (self.streamer,))
def execute(self, command, data="", fetch=False):
''' Execute Commands in SQLite '''
with sqlite3.connect(self.database_dir) as connection:
cursor = connection.cursor()
cursor.execute(command, data)
if fetch:
return cursor.fetchall()
connection.commit()
def set_death_counter(self, death_counter):
self.execute("UPDATE DeathCounter SET counter=? WHERE streamer=?", (death_counter, self.streamer))
def get_deaths(self):
return self.execute("SELECT counter FROM DeathCounter WHERE streamer=?", (self.streamer,), fetch=True)[0][0]
def increment(self):
self.execute("UPDATE DeathCounter SET counter=counter+1 WHERE streamer=?", (self.streamer,))