-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_facts.py
45 lines (28 loc) · 935 Bytes
/
db_facts.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
import sqlite3
import random
CREATE_TABLE = '''CREATE TABLE IF NOT EXISTS facts (
id INTEGER PRIMARY KEY,
fact TEXT
)
'''
INSERT = '''INSERT INTO facts (fact) VALUES (:fact) '''
DISPLAY = '''SELECT fact FROM facts WHERE id = (:id)'''
CLEAR_ALL = '''DELETE FROM facts'''
def connect():
return sqlite3.connect("facts.db")
def create_table(connection):
with connection:
connection.execute(CREATE_TABLE)
def insert(connection, cursor, fact):
with connection:
cursor.execute(INSERT, {"fact": fact})
def display_fact(cursor):
cursor.execute(DISPLAY, {"id": random.randint(0, 99)})
fact = cursor.fetchall()
return fact
def display_all(cursor):
cursor.execute("SELECT * FROM FACTS")
return cursor.fetchall()
def clear_all(connection, cursor):
with connection:
cursor.execute(CLEAR_ALL)