-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.py
48 lines (44 loc) · 1.35 KB
/
db.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
import sqlite3
import os.path
def make_db():
if not os.path.exists('leaderboard.db'):
conn = sqlite3.connect('leaderboard.db')
cur = conn.cursor()
cur.execute('''CREATE TABLE leaders
(Name Text, Score Integer, GameDate Date)
''')
conn.commit()
conn.close()
else: pass
def insert_db(player, score, game_date):
conn = sqlite3.connect('leaderboard.db')
cur = conn.cursor()
cur.execute('''
INSERT INTO leaders VALUES (?,?,?);
''', (player, score, game_date))
conn.commit()
conn.close()
def results_db():
conn = sqlite3.connect('leaderboard.db')
cur = conn.cursor()
cur.execute('''
SELECT SUBSTR(name,1,20) AS name, score, strftime('%m/%d/%Y', gamedate)
FROM leaders
ORDER BY score ASC
LIMIT 20;
''')
rows = cur.fetchall()
print '''\n\n
_ _
| | | |
| | ___ __ _ __| | ___ _ __ ___
| | / _ \/ _` |/ _` |/ _ \ '__/ __|
| |___| __/ (_| | (_| | __/ | \__ \\
\_____/\___|\__,_|\__,_|\___|_| |___/
'''
print '\n\nNAME MOVES DATE'
print '____________________________________________________\n'
for row in rows:
print '%-20s %-10s %-30s' % (row[0], str(row[1]), row[2])
print '\n\n\n'
conn.close()