-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
29 lines (26 loc) · 832 Bytes
/
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
import sqlite3
def init_db():
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS scores (
id INTEGER PRIMARY KEY,
user_id TEXT NOT NULL,
score INTEGER NOT NULL
)
''')
conn.commit()
conn.close()
def add_score(user_id, score):
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('INSERT INTO scores (user_id, score) VALUES (?, ?)', (user_id, score))
conn.commit()
conn.close()
def get_scores():
conn = sqlite3.connect('game.db')
cursor = conn.cursor()
cursor.execute('SELECT user_id, score FROM scores ORDER BY score DESC')
scores = cursor.fetchall()
conn.close()
return [{'user_id': user_id, 'score': score} for user_id, score in scores]