-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsql.py
executable file
·77 lines (66 loc) · 1.67 KB
/
sql.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
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
"""
Module for handling the messy bits of SQL.
"""
import random
import sqlite3
def get_sql():
"""
Gets a fresh sql connection and cursor to ./wordle.db
:rtype: tuple[sqlite3.Connection, sqlite3.Cursor]
"""
con: sqlite3.Connection = sqlite3.connect("wordle.db")
cur: sqlite3.Cursor = con.cursor()
return con, cur
def init_db():
"""
Initialises a new database in ./wordle.db
"""
con, cur = get_sql()
schemas = [
"""
CREATE TABLE wordList (
id INTEGER NOT NULL PRIMARY KEY,
word TEXT NOT NULL
);
""",
"""
CREATE TABLE answerList (
id INTEGER NOT NULL PRIMARY KEY,
word TEXT NOT NULL
);
""",
"""
CREATE TABLE game (
id INTEGER NOT NULL PRIMARY KEY,
word TEXT NOT NULL,
finished BOOL NOT NULL DEFAULT false,
guesses TEXT default "",
key TEXT NOT NULL,
FOREIGN KEY (word) REFERENCES wordList(word)
);
""",
]
for schema in schemas:
cur.execute(schema)
word_list = open("word_list.txt", "r").read().split("\n")
answer_list = open("answer_list.txt", "r").read().split("\n")
random.shuffle(word_list)
random.shuffle(answer_list)
for word in word_list:
cur.execute(
"""
INSERT INTO wordList (word)
VALUES (?);
""",
(word,),
)
for word in answer_list:
cur.execute(
"""
INSERT INTO answerList (word)
VALUES (?);
""",
(word,),
)
con.commit()
con.close()