-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_parent.py
239 lines (167 loc) · 8.43 KB
/
database_parent.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
import os, sys, sqlite3
import platform
class databaseClass:
def __init__(self):
self.databases = {
'cA' : {'dbCall' : 'cA', \
'dbDescription' : 'articles that have been checked already', \
'dbTableName' : 'list_checked_articles', \
'dbInit' : 'CREATE TABLE list_checked_articles(publications_ID TEXT PRIMARY KEY, file_name TEXT)'},
'f2p' : {'dbCall' : 'f2p',\
'dbDescription' : 'tables iDAI.field <- -> iDAI.pulications',\
'dbTableName' : 'field_to_publications',\
'dbInit' : 'CREATE TABLE field_to_publications(number INTEGER PRIMARY KEY AUTOINCREMENT,field_ID TEXT, publications_ID TEXT)'},
'g2p' : {'dbCall' : 'g2p',\
'dbDescription' : 'tables iDAI.gazetteer <- -> iDAI.pulications', \
'dbTableName' : 'gazetteer_to_publications',\
'dbInit' : 'CREATE TABLE gazetteer_to_publications(number INTEGER PRIMARY KEY AUTOINCREMENT, gazetteer_ID INTEGER, publications_ID TEXT)'},
'o2p' : {'dbCall' : 'o2p',\
'dbDescription' : 'tables iDAI.objects <- -> iDAI.pulications',\
'dbTableName' : 'objects_to_publications',\
'dbInit' : 'CREATE TABLE objects_to_publications(number INTEGER PRIMARY KEY AUTOINCREMENT, objects_ID INTEGER, publications_ID TEXT)'},
'z2p' : {'dbCall' : 'z2p',\
'dbDescription' : 'tables iDAI.bibliography <- -> iDAI.pulications', \
'dbTableName' : 'zenon_to_publications',\
'dbInit' : 'CREATE TABLE zenon_to_publications(number INTEGER PRIMARY KEY AUTOINCREMENT, zenon_ID TEXT, publications_ID TEXT)'},
}
self.cursor = ""
self.connection = ""
self.path = ""
self.dbPath = ""
self.dbSize = ""
self.logBuffer = []
self.queryResults = []
self.programExit = False
self.dbFromPreviousRunComplete = False
def actualize_nr_of_entries(self):
self.open()
sql = "SELECT COUNT(*) FROM list_checked_articles"
try:
self.cursor.execute(sql)
except:
errMessage = "ERROR: Cannot acces database."
self.logBuffer.append(errMessage)
print(errMessage)
sys.exit(0)
for res_table in self.cursor:
for res in res_table:
self.dbSize = res
self.close()
def check_if_db_exists(self):
self.dbFromPreviousRunComplete = False
operatingSystem = platform.system()
cwd = os.getcwd()
if operatingSystem == "Windows":
dbPathExists = os.path.exists("db_folder")
dbExists = os.path.exists("db_folder\\ID_Ex_database.db")
if operatingSystem == "Linux":
dbPathExists = os.path.exists("db_folder")
dbExists = os.path.exists("db_folder/ID_Ex_database.db")
if not dbPathExists:
message="No db_folder found.\n"
self.logBuffer.append(message)
os.makedirs("db_folder")
message="Created db_folder.\n"
self.logBuffer.append(message)
else:
message="Found db_folder.\n\n"
self.logBuffer.append(message)
if operatingSystem == "Windows":
self.path = cwd+"\\"+"db_folder"+"\\"
if operatingSystem == "Linux":
self.path = cwd+'/'+"db_folder"+'/'
if dbExists == False:
self.dbPath = self.path + "ID_Ex_database.db"
if not os.path.exists(self.dbPath):
connection = sqlite3.connect(self.dbPath)
cursor = connection.cursor()
message = "Created: " + "ID_Ex_database.db" +"\n"
self.logBuffer.append(message)
for x, y in self.databases.items():
sql = y['dbInit']
cursor.execute(sql)
connection.commit()
message = "Created Table: " + str(y['dbTableName']) +"\n"
self.logBuffer.append(message)
connection.close()
else:
self.dbPath = self.path + "ID_Ex_database.db"
self.dbFromPreviousRunComplete = True
message="Found ID_Ex_database.db.\n\n"
self.logBuffer.append(message)
def check_if_record_exists(self, articleDOI):
self.open()
articleExists = False
try:
sql = "SELECT publications_ID FROM list_checked_articles WHERE publications_ID = \"" \
+ articleDOI + "\""
self.cursor.execute(sql)
except:
errMessage = "\nERROR: Could not check database. Check and restart."
self.logBuffer.append(errMessage)
tk.messagebox.showwarning(title="ERROR", message = errMessage)
self.root.destroy()
sys.exit(0)
for cursors in self.cursor:
articleExists = True
self.close()
return articleExists
def close(self):
self.connection.commit()
self.connection.close()
def open(self):
self.connection = sqlite3.connect(self.dbPath)
self.cursor = self.connection.cursor()
def reset_for_next_run(self):
self.logBuffer = []
self.queryResults.clear()
def show_and_export_records(self, chosenDB):
self.open()
#(chosenDB was alread checked in main/check_input, so the passed argument should be valid)
for x, y in self.databases.items():
if y['dbCall'] == chosenDB:
toInsert = y['dbTableName']
sql = "SELECT * FROM" + " " + toInsert
try:
self.cursor.execute(sql)
except:
errMessage = "ERROR: Cannot acces database."
self.logBuffer.append(errMessage)
print(errMessage)
sys.exit(0)
self.queryResults.append(f"\nExport from db {toInsert}\n\n")
result = ""
for res_table in self.cursor:
for res in res_table:
result = result + str(res) + ", "
if len(result)>2:
result = result[:-2] #cut off the final comma for a clean list
result = result + "\n"
queryResult = f"{result}\n"
self.queryResults.append(queryResult)
result = ""
self.close()
def update(self, containerArticles):
self.open()
for containerArticle in containerArticles:
self.cursor.execute("INSERT OR REPLACE INTO list_checked_articles VALUES(?, ?);", \
(containerArticle.articleDOI, containerArticle.fileName))
for containerArticle in containerArticles:
for objectsID in containerArticle.objectsIDs:
self.cursor.execute("INSERT OR REPLACE INTO objects_to_publications VALUES(NULL, ?, ?);", \
(objectsID, containerArticle.articleDOI,))
containerArticle.logBuffer.append(f"objectsID extracted: {objectsID}")
for zenonID in containerArticle.zenonIDs:
self.cursor.execute("INSERT OR REPLACE INTO zenon_to_publications VALUES(NULL, ?, ?);", \
(zenonID, containerArticle.articleDOI,))
containerArticle.logBuffer.append(f"zenonID extracted: {zenonID}")
for gazetteerID in containerArticle.gazetteerIDs:
self.cursor.execute("INSERT OR REPLACE INTO gazetteer_to_publications VALUES(NULL, ?, ?);", \
(gazetteerID, containerArticle.articleDOI,))
containerArticle.logBuffer.append(f"gazetteerID extracted: {gazetteerID}")
for fieldID in containerArticle.fieldIDs:
self.cursor.execute("INSERT OR REPLACE INTO field_to_publications VALUES(NULL, ?, ?);", \
(fieldID, containerArticle.articleDOI,))
containerArticle.logBuffer.append(f"fieldID extracted: {fieldID}")
self.close()
self.actualize_nr_of_entries()