-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_manager.py
More file actions
53 lines (47 loc) · 1.63 KB
/
database_manager.py
File metadata and controls
53 lines (47 loc) · 1.63 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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# database_manager.py
import sqlite3
DATABASE_FILE = "subscribers.db"
def initialize_database():
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS subscribers (contact_name TEXT PRIMARY KEY)")
conn.commit()
conn.close()
print("Banco de dados 'subscribers.db' pronto para uso.")
def add_subscriber(contact_name):
try:
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute("INSERT OR IGNORE INTO subscribers (contact_name) VALUES (?)", (contact_name,))
conn.commit()
conn.close()
print(f"✅ Assinante '{contact_name}' foi ADICIONADO.")
return True
except Exception as e:
print(f"❌ Erro ao adicionar: {e}")
return False
def remove_subscriber(contact_name):
try:
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute("DELETE FROM subscribers WHERE contact_name = ?", (contact_name,))
conn.commit()
conn.close()
print(f"🗑️ Assinante '{contact_name}' foi REMOVIDO.")
return True
except Exception as e:
print(f"❌ Erro ao remover: {e}")
return False
def get_all_subscribers():
try:
conn = sqlite3.connect(DATABASE_FILE)
cursor = conn.cursor()
cursor.execute("SELECT contact_name FROM subscribers")
subscribers = [row[0] for row in cursor.fetchall()]
conn.close()
return subscribers
except Exception as e:
print(f"❌ Erro ao buscar: {e}")
return []
if __name__ == "__main__":
initialize_database()