-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_creation.py
More file actions
223 lines (178 loc) · 8.41 KB
/
database_creation.py
File metadata and controls
223 lines (178 loc) · 8.41 KB
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
import sqlite3
import os
from random import *
from faker import Faker
import const
from logger import Logger
import time
class createDatabase:
def __init__(self):
self.createDB()
def createDB(self):
if os.path.exists(const.database_name):
try:
os.remove(const.database_name)
Logger.log("Old Database is deleted.")
except PermissionError as win32_error:
Logger.raise_error(win32_error)
Logger.log("Program will be sleep 10 second.")
time.sleep(10)
Logger.log("Program started again.")
self.createDB()
connectionDB = sqlite3.connect(const.database_name)
cursor = connectionDB.cursor()
cursor.execute("PRAGMA foreign_keys = ON")
Logger.log("Database is created.")
try:
cursor.execute("CREATE TABLE IF NOT EXISTS CITIZEN("
"CitizenID INTEGER PRIMARY KEY,"
"Name TEXT,"
"Surname TEXT,"
"DOB DATE,"
"Adress TEXT,"
"FingerPrint BLOB,"
"Photo BLOB)")
cursor.execute("CREATE TABLE IF NOT EXISTS Election("
"ElectionID INTEGER PRIMARY KEY AUTOINCREMENT,"
"Result TEXT,"
"DateOfElection DATE,"
"ElectionTime TIME,"
"Description TEXT,"
"EndDate DATE,"
"EndTime TIME)")
cursor.execute("CREATE TABLE IF NOT EXISTS Vote("
"Isvoted BOOLEAN,"
"CitizenID INTEGER,"
"ElectionID INTEGER,"
"FOREIGN KEY (CitizenID) REFERENCES CITIZEN(CitizenID),"
"FOREIGN KEY (ElectionID) REFERENCES Election(ElectionID),"
"PRIMARY KEY(CitizenID, ElectionID))")
cursor.execute("CREATE TABLE IF NOT EXISTS Admin("
"CitizenID INTEGER,"
"FOREIGN KEY (CitizenID) REFERENCES CITIZEN(CitizenID),"
"PRIMARY KEY(CitizenID))")
cursor.execute("CREATE TABLE IF NOT EXISTS Candidate("
"CitizenID INTEGER,"
"FOREIGN KEY (CitizenID) REFERENCES CITIZEN(CitizenID),"
"PRIMARY KEY(CitizenID))")
cursor.execute("CREATE TABLE IF NOT EXISTS CandidateElection("
"CountOfVote INTEGER,"
"CitizenID INTEGER,"
"ElectionID INTEGER,"
"FOREIGN KEY (CitizenID) REFERENCES Candidate(CitizenID),"
"FOREIGN KEY (ElectionID) REFERENCES Election(ElectionID),"
"PRIMARY KEY(CitizenID, ElectionID))")
Logger.log("Tables are created.")
except sqlite3.OperationalError or sqlite3.DatabaseError:
Logger.raise_error("Tables cannot created.")
connectionDB.commit()
cursor.close()
connectionDB.close()
class InsertionRecord:
def __init__(self, path, face):
# ---------Helper Variable---------------
self.images_name = []
self.faces_name = []
self.images = []
self.faces = []
self.path = path
self.path2 = face
self.faker = Faker()
# ---------------------------------------
# -------Citizen Variable----------------
self.national_ID = None
self.first_name = None
self.last_name = None
self.birth_date = None
self.city = None
@staticmethod
def run():
createDatabase()
DB_insert_Record.readImage()
DB_insert_Record.insertRecord()
Logger.log("All processes are completed.")
def readImage(self):
try: # Reading fingerprint names to find this image path
for image_path in os.listdir(self.path):
self.images_name.append(image_path)
except OSError as error:
Logger.raise_error(error)
try: # Reading fingerprint as binary format and append to list
for image_name in self.images_name:
with open(self.path + "//" + image_name, "rb") as image:
binary_data = image.read()
self.images.append(binary_data)
Logger.log("Finger prints are reading successfully")
except Exception as error:
Logger.raise_error(error)
try: # Reading face image names to find this image path
for face_path in os.listdir(self.path2):
self.faces_name.append(face_path)
except Exception as error:
Logger.raise_error(error)
try: # Reading face image as binary format and append to list
for face_name in self.faces_name:
with open(self.path2 + "//" + face_name, "rb") as face:
binary_data = face.read()
self.faces.append(binary_data)
Logger.log("Faces are reading successfully")
except Exception as error:
Logger.raise_error(error)
def insertRecord(self):
connectionDB = sqlite3.connect(const.database_name)
cursor = connectionDB.cursor()
for index_num in range(len(const.citizen)):
cursor.execute("INSERT INTO Citizen VALUES(?, ?, ?, ?, ?, ?, ?)",
(const.citizen[index_num] + (self.images[index_num],) + (self.faces[index_num],)))
Logger.log("Real people are inserted Citizen table.")
for i in range(80):
self.random_Citizen_Info()
cursor.execute("INSERT INTO Citizen VALUES(?, ?, ?, ?, ?, ?, ?)",
(self.national_ID, self.first_name, self.last_name,
self.birth_date, self.city, self.images[i], self.faces[i]))
Logger.log("Mock people are inserted Citizen tabel.")
# ------Admin------
cursor.execute("INSERT INTO Admin (CitizenID) SELECT CitizenID FROM Citizen WHERE CitizenID = ?",
(const.citizen[0][0],))
Logger.log("Mock admins are inserted.")
# ------Candidate------
cursor.executemany("INSERT INTO Candidate (CitizenID) SELECT CitizenID FROM Citizen WHERE CitizenID = ?",
const.candidate)
Logger.log("Mock candidates are inserted.")
# ------CandidateElection------
cursor.executemany("INSERT INTO CandidateElection (CountOfVote, CitizenID, ElectionID) VALUES (?,?,?)",
const.candidate_election)
Logger.log("Mock candidate_table are inserted.")
# ------Election------
cursor.executemany(
"INSERT INTO Election (ElectionID, Result, DateOfElection, ElectionTime, EndDate, EndTime) VALUES (?, ?, ?, ?,?,?)",
const.election)
Logger.log("Mock elections are inserted.")
# ------Vote------
cursor.execute("INSERT INTO Vote (CitizenID, IsVoted) SELECT CitizenID, ? FROM Citizen WHERE CitizenID",
(False,))
Logger.log("Mock Vote are inserted.")
connectionDB.commit()
cursor.close()
connectionDB.close()
def random_Citizen_Info(self):
self.national_ID = randint(10000000000, 99999999999)
self.first_name = self.faker.first_name()
self.last_name = self.faker.last_name()
self.birth_date = self.faker.date_of_birth(minimum_age=18, maximum_age=100).strftime("%Y-%m-%d")
self.city = choice(["Adana", "Bursa", "Konya", "Eskişehir"])
if __name__ == "__main__":
DB_insert_Record = InsertionRecord(const.path_of_image, const.path_of_face)
DB_insert_Record.run()
"""if os.path.exists(const.database_name):
try:
cursor.execute("DELETE FROM Citizen")
cursor.execute("DELETE FROM Admin")
cursor.execute("DELETE FROM Candidate")
cursor.execute("DELETE FROM CandidateElection")
cursor.execute("DELETE FROM Election")
cursor.execute("DELETE FROM Vote")
connectionDB.commit()
Logger.log("")
except sqlite3.OperationalError:
DataBaseCreation.createDatabase()"""