-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
193 lines (135 loc) · 4.12 KB
/
db.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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import sqlite3
import sys
import os.path
db_name = "info.db"
init_src = "tsumo.txt"
new_tsumo_file = "new_tsumo.txt"
keys_src = "keys"
all_tsumo = "all_tsumo.txt"
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
db_path = os.path.join(BASE_DIR, db_name)
def create_tables():
tsumo_lines = []
keys_lines = []
with open(init_src, "r") as f:
tsumo_lines = f.readlines()
with open(keys_src, "r") as f:
keys_lines = f.readlines()
conn = sqlite3.connect(db_path)
c = conn.cursor()
#Create the Tsumo table and poulate it with Tsumos
c.execute('''CREATE table Tsumo
(ID integer primary key, tsumo text, meaning text)''')
for line in tsumo_lines:
c.execute('''INSERT INTO Tsumo(tsumo) values (?)''',(line[:-1],))
#Create the keys table and populate it with Keys
c.execute('''CREATE TABLE Keys
(key text, value text)''')
for line in keys_lines:
if len(line) == 0:
continue
c.execute('''INSERT INTO Keys (key, value) values (?, ?)''', (line.split(": ")[0], line.split(": ")[1][:-1]))
conn.commit()
conn.close()
def get_tsumo(x):
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''SELECT tsumo FROM Tsumo WHERE ID = ?''', (x,))
result = c.fetchone()
conn.close()
return str(result[0])
def get_tsumo_count():
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''SELECT count(*) FROM Tsumo''')
result = c.fetchone()
return int(result[0])
def print_all_tsumo():
conn = sqlite3.connect(db_path)
c = conn.cursor()
tsumo = []
c.execute("""SELECT * FROM Tsumo""")
with open(all_tsumo, "w") as f:
for result in c.fetchall():
f.write(result[1] + "\n")
conn.close()
def new_tsumo():
print_all_tsumo()
new_lines = []
new_tsumo = []
with open(new_tsumo_file, "r") as f:
new_lines = f.readlines()
for line in new_lines:
if len(line) < 2:
continue
if line.find("|") == -1:
new_tsumo.append([line[:-1], ""])
else:
new_tsumo.append(line[:-1].split("|"))
print (new_tsumo)
conn = sqlite3.connect(db_path)
c = conn.cursor()
x = c.rowcount
print(x)
for tsumo in new_tsumo:
c.execute('''INSERT INTO Tsumo(tsumo, meaning)
SELECT ?, ?
WHERE NOT EXISTS( SELECT 1
FROM Tsumo
WHERE tsumo == ?)''', (tsumo[0], tsumo[1], tsumo[0]))
conn.commit()
conn.close()
y = c.rowcount
print(y)
if x ==y:
print ("No new rows were added.")
else:
print(y-x, " rows were added.")
with open(new_tsumo, "w") as f:
f.write("")
def get_keys():
keys = {}
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''SELECT key, value FROM Keys''')
results = c.fetchall()
for result in results:
keys[result[0]] = result[1]
conn.commit()
conn.close()
return keys
def delete_table():
print_all_tsumo()
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute('''DROP TABLE Tsumo''')
conn.commit()
conn.close()
def clean_tsumo():
print_all_tsumo()
conn = sqlite3.connect(db_path)
c = conn.cursor()
c.execute(''' DELETE FROM Tsumo
WHERE tsumo = ""''')
conn.commit()
conn.close()
# Running db methods
if __name__ == "__main__":
if len(sys.argv) < 2:
print("No option selected. Exitting.")
elif sys.argv[1] == "create_tables":
create_tables()
elif sys.argv[1] == "new_tsumo":
print("Adding new tsumo to the database.")
new_tsumo()
elif sys.argv[1] == "delete_table":
print ("Dropping Tsumo database")
delete_table()
elif sys.argv[1] == "back_up":
print("Back up selected. Backing up data...")
print_all_tsumo()
elif sys.argv[1] == "clean_tsumo":
print("Cleaning blank tsumo from DB...")
clean_tsumo()
else:
print("Unavailable option.")