-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdatabase.py
More file actions
88 lines (78 loc) · 2.31 KB
/
Copy pathdatabase.py
File metadata and controls
88 lines (78 loc) · 2.31 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import sqlite3
from config import DB_FILE
def init_db():
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute("""CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
api_key TEXT NOT NULL
)""")
cur.execute("""CREATE TABLE IF NOT EXISTS messages (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
room TEXT NOT NULL,
content TEXT,
timestamp TEXT DEFAULT CURRENT_TIMESTAMP,
private INTEGER DEFAULT 0
)""")
con.commit()
con.close()
def register_user(username, password_hash, api_key):
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
try:
cur.execute(
"INSERT INTO users (username, password_hash, api_key) VALUES (?,?,?)",
(username, password_hash, api_key)
)
con.commit()
return True
except sqlite3.IntegrityError:
return False
finally:
con.close()
def get_user(username):
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute("SELECT * FROM users WHERE username = ?", (username,))
result = cur.fetchone()
con.close()
return result
def get_user_by_api_key(api_key):
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute("SELECT * FROM users WHERE api_key = ?", (api_key,))
result = cur.fetchone()
con.close()
return result
def save_message(username, room, content, private=0):
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute(
"INSERT INTO messages (username, room, content, private) VALUES (?,?,?,?)",
(username, room, content, private)
)
con.commit()
con.close()
# ✅ FIXED FUNCTION
def get_messages(room, limit=50):
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute("""
SELECT username, content, timestamp
FROM messages
WHERE room = ?
ORDER BY id ASC
""", (room,))
result = cur.fetchall()
con.close()
return result[-limit:] # last N messages in correct order
def get_all_users():
con = sqlite3.connect(DB_FILE)
cur = con.cursor()
cur.execute("SELECT username FROM users")
result = cur.fetchall()
con.close()
return [r[0] for r in result]