-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
107 lines (97 loc) · 3.19 KB
/
database.py
File metadata and controls
107 lines (97 loc) · 3.19 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import sqlite3
import os
DB_FILE = 'starcraft_matches.db'
def init_db():
print("Initializing database...")
conn = None
try:
conn = sqlite3.connect(DB_FILE)
c = conn.cursor()
c.execute('''
CREATE TABLE IF NOT EXISTS matches (
id INTEGER PRIMARY KEY AUTOINCREMENT,
opponent_name TEXT,
map_name TEXT,
wins INTEGER,
losses INTEGER,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
''')
print("matches table checked/created")
c.execute('''
CREATE TABLE IF NOT EXISTS user_info (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT UNIQUE,
replay_path TEXT
)
''')
print("user_info table checked/created")
conn.commit()
print("Database initialized and tables created/checked")
except sqlite3.Error as e:
print(f"An error occurred: {e}")
finally:
if conn:
conn.close()
def get_connection():
"""Create a database connection to the SQLite database."""
try:
conn = sqlite3.connect(DB_FILE)
return conn
except sqlite3.Error as e:
print(f"Error connecting to database: {e}")
return None
def fetch_data(query, params=()):
"""Fetch data from the database using the provided query and parameters."""
conn = get_connection()
if conn:
try:
cur = conn.cursor()
cur.execute(query, params)
rows = cur.fetchall()
return rows
except sqlite3.Error as e:
print(f"Error executing query: {e}")
return None
finally:
conn.close()
def save_to_db(opponent_name, map_name, wins, losses):
conn = get_connection()
if conn:
try:
c = conn.cursor()
c.execute('''
INSERT INTO matches (opponent_name, map_name, wins, losses)
VALUES (?, ?, ?, ?)
''', (opponent_name, map_name, wins, losses))
conn.commit()
print("Data saved successfully")
except sqlite3.Error as e:
print(f"Error saving to database: {e}")
finally:
conn.close()
def save_user_info(username, replay_path):
conn = get_connection()
if conn:
try:
c = conn.cursor()
c.execute('INSERT OR REPLACE INTO user_info (id, username, replay_path) VALUES (1, ?, ?)', (username, replay_path))
conn.commit()
print("User info saved successfully")
except sqlite3.Error as e:
print(f"Error saving user info to database: {e}")
finally:
conn.close()
def get_user_info():
conn = get_connection()
if conn:
try:
c = conn.cursor()
c.execute('SELECT username, replay_path FROM user_info WHERE id = 1')
result = c.fetchone()
return result if result else (None, None)
except sqlite3.Error as e:
print(f"Error retrieving user info from database: {e}")
return None, None
finally:
conn.close()