-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
57 lines (45 loc) · 1.08 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import sqlite3
def create_db():
db = sqlite3.connect('snake.sqlite')
cur = db.cursor()
cur.execute("""
create table if not exists SCORES (
players text,
scores integer
)
""")
cur.execute("""
create table if not exists RECENT (
players text
)
""")
def get_best():
db = sqlite3.connect('snake.sqlite')
cur = db.cursor()
cur.execute("""
SELECT players, scores from SCORES
ORDER by scores DESC
LIMIT 5 """)
s = cur.fetchall()
db.close()
return s
def get_recent_player():
db = sqlite3.connect('snake.sqlite')
cur = db.cursor()
recent_player = None
cur.execute("""
SELECT players from RECENT
ORDER by ROWID DESC
LIMIT 1 """)
for player in cur.fetchall():
recent_player = player[0]
db.close()
return recent_player
def write_scores(player, score):
db = sqlite3.connect('snake.sqlite')
cur = db.cursor()
cur.execute(
f"INSERT INTO SCORES (players, scores) VALUES ('{player}', '{score}')")
cur.execute(f"INSERT INTO RECENT (players) VALUES ('{player}')")
db.commit()
db.close()