-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_control.py
46 lines (42 loc) · 1.57 KB
/
db_control.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
import sqlite3
import pandas as pd
class ScoreDatabase:
def __init__(self, db_name="my.db"):
self.db_name = db_name
self._initialize_database()
def _initialize_database(self):
"""Private method to ensure the scores table is created."""
with sqlite3.connect(self.db_name) as conn:
create_table = """
CREATE TABLE IF NOT EXISTS scores (
id INTEGER PRIMARY KEY,
submissionText TEXT,
feedback TEXT,
score INT
);
"""
cursor = conn.cursor()
cursor.execute(create_table)
conn.commit()
def show_scores(self):
"""Displays all records from the scores table."""
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute("SELECT * FROM scores")
records = cursor.fetchall()
return records
def insert_score(self, submissionText, feedback, score):
"""Inserts a new record into the scores table."""
with sqlite3.connect(self.db_name) as conn:
cursor = conn.cursor()
cursor.execute(
"INSERT INTO scores (submissionText, feedback, score) VALUES (?, ?, ?)",
(submissionText, feedback, score)
)
conn.commit()
def fetch_scores_from_db():
conn = sqlite3.connect('my.db')
query = "SELECT submissionId, score FROM scores"
df = pd.read_sql_query(query, conn)
conn.close()
return df