-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlController.py
More file actions
36 lines (29 loc) · 1.41 KB
/
sqlController.py
File metadata and controls
36 lines (29 loc) · 1.41 KB
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
import sqlite3
class SQLighter:
"""
This class represent a simple database controller
"""
def __init__(self, database):
"""Connect to the database and save the connection cursor"""
self.connection = sqlite3.connect(database)
self.cursor = self.connection.cursor()
def get_subscriptions(self, status = True):
"""Get all active subscribers of the bot"""
with self.connection:
return self.cursor.execute("SELECT * FROM `subscriptions` WHERE `status` = ?", (status,)).fetchall()
def subscriber_exists(self, user_id):
"""Check if there is already a user in the database"""
with self.connection:
result = self.cursor.execute('SELECT * FROM `subscriptions` WHERE `user_id` = ?', (user_id,)).fetchall()
return bool(len(result))
def add_subscriber(self, user_id, status = True):
"""Add a new subscriber"""
with self.connection:
return self.cursor.execute("INSERT INTO `subscriptions` (`user_id`, `status`) VALUES(?,?)", (user_id,status))
def update_subscription(self, user_id, status):
"""Updating the user's subscription status"""
with self.connection:
return self.cursor.execute("UPDATE `subscriptions` SET `status` = ? WHERE `user_id` = ?", (status, user_id))
def close(self):
"""Closing the database connection"""
self.connection.close()