-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
49 lines (44 loc) · 1.59 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
import sqlite3
from datetime import datetime
def init_db():
conn = sqlite3.connect('textbook_qa.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS queries
(id INTEGER PRIMARY KEY AUTOINCREMENT,
textbook TEXT,
query TEXT,
answer TEXT,
timestamp DATETIME)''')
c.execute('''CREATE TABLE IF NOT EXISTS textbooks
(id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT,
structure TEXT)''')
conn.commit()
conn.close()
def save_query(textbook, query, answer):
conn = sqlite3.connect('textbook_qa.db')
c = conn.cursor()
c.execute("INSERT INTO queries (textbook, query, answer, timestamp) VALUES (?, ?, ?, ?)",
(textbook, query, answer, datetime.now()))
conn.commit()
conn.close()
def get_query_history():
conn = sqlite3.connect('textbook_qa.db')
c = conn.cursor()
c.execute("SELECT textbook, query, answer, timestamp FROM queries ORDER BY timestamp DESC")
history = c.fetchall()
conn.close()
return history
def save_textbook_structure(name, structure):
conn = sqlite3.connect('textbook_qa.db')
c = conn.cursor()
c.execute("INSERT INTO textbooks (name, structure) VALUES (?, ?)", (name, structure))
conn.commit()
conn.close()
def get_textbook_structure(name):
conn = sqlite3.connect('textbook_qa.db')
c = conn.cursor()
c.execute("SELECT structure FROM textbooks WHERE name = ?", (name,))
result = c.fetchone()
conn.close()
return result[0] if result else None