-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcreate_sqlite3_database.py
More file actions
386 lines (336 loc) · 14 KB
/
create_sqlite3_database.py
File metadata and controls
386 lines (336 loc) · 14 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import sqlite3
import os
import csv
import re
import time
from datetime import datetime
# Configuration and tracking variables
raw_data_dir = 'raw_data'
files_processed = 0
total_lines = 0
total_time = 0
database_name = 'EmployeeDB.db'
# Database initialization and schema creation
conn = sqlite3.connect(database_name)
c = conn.cursor()
# Person table stores unique individuals and their most recent employment details
c.execute('''
CREATE TABLE IF NOT EXISTS Person (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
FirstName TEXT NOT NULL,
LastName TEXT NOT NULL,
MostRecentTitle TEXT,
MostRecentEmployer TEXT,
MostRecentEntryDate DATE
)
''')
# Indexes to optimize person lookups by name
print("Creating indexes...")
c.execute('''CREATE INDEX IF NOT EXISTS idx_person_firstname
ON Person(FirstName)''')
c.execute('''CREATE INDEX IF NOT EXISTS idx_person_lastname
ON Person(LastName)''')
c.execute('''CREATE INDEX IF NOT EXISTS idx_person_fullname
ON Person(FirstName, LastName)''')
# Salary table stores all salary records with foreign key to Person
c.execute('''
CREATE TABLE IF NOT EXISTS Salary (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
PersonID INTEGER,
Title TEXT,
Employer TEXT,
Salary REAL,
Bonus REAL,
TotalPay REAL,
EntryDate DATE,
SourceFile TEXT,
LineNumber INTEGER,
FOREIGN KEY (PersonID) REFERENCES Person(ID)
)
''')
# Indexes to optimize salary record lookups and joins
print("Creating Salary table indexes...")
c.execute('''CREATE INDEX IF NOT EXISTS idx_salary_personid
ON Salary(PersonID)''')
c.execute('''CREATE INDEX IF NOT EXISTS idx_salary_sourcefile
ON Salary(SourceFile)''')
c.execute('''CREATE INDEX IF NOT EXISTS idx_salary_employer
ON Salary(Employer)''')
# DataFiles table tracks processed files and their statistics
print("Creating DataFiles table...")
c.execute('''
CREATE TABLE IF NOT EXISTS DataFiles (
ID INTEGER PRIMARY KEY AUTOINCREMENT,
FileName TEXT NOT NULL,
Rows INTEGER,
FileDate DATE,
HasHeader BOOLEAN DEFAULT 0
)
''')
# Create index for DataFiles table
c.execute('''CREATE INDEX IF NOT EXISTS idx_datafiles_filename
ON DataFiles(FileName)''')
# Commit the changes
conn.commit()
# Reset database for fresh import
print("Clearing existing data...")
c.execute('DELETE FROM Salary')
c.execute('DELETE FROM Person')
c.execute('DELETE FROM DataFiles')
c.execute('DELETE FROM SQLITE_SEQUENCE WHERE name IN ("Salary", "Person", "DataFiles")') # Reset autoincrement counters
conn.commit()
# Helper function for parsing dates from filenames
def parse_date_from_filename(filename):
"""Parse date from filename supporting multiple formats:
- mm-dd-yyyy or m-d-yyyy
- mmddyyyy
- mm.dd.yyyy or m.d.yyyy
- mm-yyyy (uses 1st as day)
- mm.yyyy (uses 1st as day)
"""
date_patterns = [
r'\d{1,2}-\d{1,2}-\d{4}', # mm-dd-yyyy or m-d-yyyy
r'\d{8}', # mmddyyyy
r'\d{1,2}\.\d{1,2}\.\d{4}', # mm.dd.yyyy or m.d.yyyy
r'\d{1,2}-\d{4}', # mm-yyyy
r'\d{1,2}\.\d{4}' # mm.yyyy
]
for pattern in date_patterns:
match = re.search(pattern, filename)
if match:
date_str = match.group()
try:
if '-' in date_str:
if len(date_str.split('-')) == 2: # mm-yyyy format
month, year = date_str.split('-')
return datetime(int(year), int(month), 1).date()
return datetime.strptime(date_str, '%m-%d-%Y' if len(date_str.split('-')[0]) == 2 else '%m-%d-%Y').date()
elif '.' in date_str:
if len(date_str.split('.')) == 2: # mm.yyyy format
month, year = date_str.split('.')
return datetime(int(year), int(month), 1).date()
return datetime.strptime(date_str, '%m.%d.%Y' if len(date_str.split('.')[0]) == 2 else '%m.%d.%Y').date()
else:
return datetime.strptime(date_str, '%m%d%Y').date()
except ValueError as e:
print(f"Warning: Could not parse date from {date_str}: {e}")
return None
return None
# Helper function for converting pay values to floats
def convert_pay_value(value_str):
"""Convert pay string to float, handling:
- Dollar signs and commas
- Parentheses for negative numbers
- Empty or invalid values
"""
value_str = value_str.strip()
is_negative = value_str.startswith('(') and value_str.endswith(')')
# Remove $, commas, and parentheses
clean_value = value_str.replace('$', '').replace(',', '').replace('(', '').replace(')', '')
try:
value = float(clean_value)
return -value if is_negative else value
except ValueError as e:
raise ValueError(f"Could not convert {value_str} to number: {e}")
def is_header_row(row):
"""Detect header rows by checking for:
- Common header terms
- Absence of numeric values
- Standard header patterns
"""
header_terms = ['name', 'employee', 'title', 'salary', 'pay', 'bonus', 'employer']
has_header_terms = any(term in ' '.join(row).lower() for term in header_terms)
has_numbers = any(any(char.isdigit() for char in cell) for cell in row)
return has_header_terms and not has_numbers
def process_pay_values(pay_values, first_name, last_name):
"""Process pay values from CSV row and return salary, bonus, and total pay."""
if len(pay_values) == 0:
print(f"No valid pay data found for {first_name} {last_name}")
return None, None, None
if len(pay_values) == 1:
# Single value case - treat as total pay
return None, None, convert_pay_value(pay_values[0])
# Multiple values case
salary = convert_pay_value(pay_values[0])
if len(pay_values) > 2:
bonus = convert_pay_value(pay_values[1])
total_pay = convert_pay_value(pay_values[2])
else:
bonus = None
total_pay = convert_pay_value(pay_values[-1])
return salary, bonus, total_pay
def process_row(row, filename, reader_line_num):
"""Process a single row from the CSV file and return the processed values."""
# Clean any potential invalid characters from strings
cleaned_row = []
for cell in row:
try:
cleaned_cell = cell.encode('utf-8', 'ignore').decode('utf-8')
if cleaned_cell != cell:
print(f"Warning: Invalid characters removed from cell in {filename}, row {reader_line_num}")
cleaned_row.append(cleaned_cell)
except UnicodeError as e:
print(f"Warning: Unicode error in {filename}, row {reader_line_num}: {e}")
cleaned_row.append('')
row = cleaned_row
# Extract basic information
last_name = row[0].strip() if row[0].strip() else 'Unknown'
first_name = row[1].strip() if row[1].strip() else 'Unknown'
employer = row[2].strip() if row[2].strip() else 'Unknown'
title = row[3].strip() if row[3].strip() else 'Unknown'
# Handle pay values
if len(row) < 5:
print(f"Insufficient data columns for {first_name} {last_name}")
return None
# Remove any trailing commas and empty cells
pay_values = [cell.strip() for cell in row[4:] if cell.strip() and not cell.strip().endswith(',')]
salary, bonus, total_pay = process_pay_values(pay_values, first_name, last_name)
if total_pay is None:
return None
return {
'first_name': first_name,
'last_name': last_name,
'employer': employer,
'title': title,
'salary': salary,
'bonus': bonus,
'total_pay': total_pay,
'line_number': reader_line_num
}
# Main processing loop for all CSV files
for filename in os.listdir(raw_data_dir):
if filename.endswith('.csv'):
file_path = os.path.join(raw_data_dir, filename)
entry_date = parse_date_from_filename(filename)
rows_processed = 0
start_time = time.time() # Start timing for this file
print(f"\nProcessing file: {filename}")
print(f"File Date: {entry_date}")
try:
# First try UTF-8 with BOM
with open(file_path, 'r', encoding='utf-8-sig', newline='') as csvfile:
reader = csv.reader(csvfile)
first_row = next(reader)
# Check if first row is a header and store result
has_header = is_header_row(first_row)
if has_header:
print(f"Skipping header row in {filename}")
else:
csvfile.seek(0) # Reset to start if no header
try:
line_count = 0
for row in reader:
line_count += 1
if line_count % 10000 == 0:
print(f"Processing line {line_count} in {filename}")
# Skip empty rows or if this is another header-like row
if not row or all(cell.strip() == '' for cell in row) or (has_header and is_header_row(row)):
continue
# Process the row
processed_data = process_row(row, filename, reader.line_num)
if processed_data is None:
continue
# Check if person already exists
c.execute('SELECT ID FROM Person WHERE FirstName = ? AND LastName = ?',
(processed_data['first_name'], processed_data['last_name']))
person = c.fetchone()
if person:
person_id = person[0]
else:
# Insert new person
c.execute('''
INSERT INTO Person (FirstName, LastName, MostRecentTitle, MostRecentEmployer, MostRecentEntryDate)
VALUES (?, ?, ?, ?, ?)''',
(processed_data['first_name'], processed_data['last_name'],
processed_data['title'], processed_data['employer'], entry_date))
person_id = c.lastrowid
# Insert salary record
c.execute('''
INSERT INTO Salary (PersonID, Title, Employer, Salary, Bonus, TotalPay, EntryDate, SourceFile, LineNumber)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)''',
(person_id, processed_data['title'], processed_data['employer'],
processed_data['salary'], processed_data['bonus'], processed_data['total_pay'],
entry_date, filename, processed_data['line_number']))
rows_processed += 1
except UnicodeDecodeError as e:
print(f"Warning: Unicode decode error in {filename}: {e}")
continue
except UnicodeDecodeError as e:
print(f"Error: Unable to process {filename} with UTF-8 encoding: {e}")
try:
# Fallback to Latin-1 encoding
print(f"Attempting to read {filename} with Latin-1 encoding")
with open(file_path, 'r', encoding='latin-1', newline='') as csvfile:
# ... same processing code as above ...
pass
except Exception as e:
print(f"Error: Failed to process {filename} with Latin-1 encoding: {e}")
continue
# After file is processed, calculate and display metrics
end_time = time.time()
processing_time = end_time - start_time
lines_per_second = rows_processed / processing_time if processing_time > 0 else 0
print(f"\nFile Processing Complete:")
print(f"File: {filename}")
print(f"Rows processed: {rows_processed}")
print(f"Processing time: {processing_time:.2f} seconds")
print(f"Performance: {lines_per_second:.2f} lines/second")
# Store file information
c.execute('''
INSERT INTO DataFiles (FileName, Rows, FileDate, HasHeader)
VALUES (?, ?, ?, ?)
''', (filename, rows_processed, entry_date, 1 if has_header else 0))
# Update totals
total_lines += rows_processed
total_time += processing_time
files_processed += 1
# Commit changes for each file
conn.commit()
# Before closing connection, add summary queries
print("\nProcessing Summary:")
print("-----------------")
print(f"Files Processed: {files_processed}")
print(f"Total Lines Processed: {total_lines}")
print(f"Total Processing Time: {total_time:.2f} seconds")
print(f"Overall Performance: {(total_lines/total_time if total_time > 0 else 0):.2f} lines/second")
# Get total number of people
c.execute('SELECT COUNT(*) FROM Person')
total_people = c.fetchone()[0]
# Get total number of salary records
c.execute('SELECT COUNT(*) FROM Salary')
total_salaries = c.fetchone()[0]
print(f"Total People: {total_people}")
print(f"Total Salary Records: {total_salaries}")
# Update each person's record with their most recent employment information from all salary entries
print("\nUpdating Person records with most recent information...")
c.execute('''
UPDATE Person
SET
MostRecentTitle = (
SELECT s.Title
FROM Salary s
WHERE s.PersonID = Person.ID
ORDER BY s.EntryDate DESC
LIMIT 1
),
MostRecentEmployer = (
SELECT s.Employer
FROM Salary s
WHERE s.PersonID = Person.ID
ORDER BY s.EntryDate DESC
LIMIT 1
),
MostRecentEntryDate = (
SELECT s.EntryDate
FROM Salary s
WHERE s.PersonID = Person.ID
ORDER BY s.EntryDate DESC
LIMIT 1
)
''')
# Commit the final updates
conn.commit()
print(f"Updated {c.rowcount} Person records with most recent information")
# Close the connection
conn.close()
print('\nDatabase created successfully.')