-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.py
More file actions
83 lines (64 loc) · 2.07 KB
/
Copy pathutilities.py
File metadata and controls
83 lines (64 loc) · 2.07 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
import csv
def getCsvDataQuery(file_path):
query = "INSERT INTO"
if ('admins.csv' in file_path):
query += " Administrators "
elif ('courses.csv' in file_path):
query += " Courses "
elif ('emails.csv' in file_path):
query += " UserEmail "
elif ('machines.csv' in file_path):
query += " Machines "
elif ('manage.csv' in file_path):
query += " AdministratorManageMachines "
elif ('projects.csv' in file_path):
query += " Projects "
elif ('students.csv' in file_path):
query += " Students "
elif ('use.csv' in file_path):
query += " StudentUseMachinesInProject "
elif ('users.csv' in file_path):
query += " Users "
query += "VALUES \n"
with open(file_path, 'r') as file:
csv_reader = csv.reader(file)
for line in csv_reader:
query += "("
for value in line:
query += "\'" + value + "\', "
query = query[:-2] + "), \n"
query = query[:-3] + ";"
return query
def execute_command(db_connection, cursor, sql_command):
'''
Executes sql command in connection.
:return: Bool, optional results
'''
try:
# print("Successfully connected to the database")
# print("Initialization begin")
cursor.execute(sql_command)
result = cursor.fetchall()
# print(result)
db_connection.commit()
# print("Initialization end successfully")
return "Success", result
except Exception as e:
# print(e)
return 'Fail', []
def printRows(result):
'''
Formats result printing from table to csv
- arg: result table
- returns: None
'''
if result[0] == 'Fail':
print(result[0])
elif result[0] == 'Success':
for record in result[1]:
formatted_record = ','.join(str(value) for value in record)
print(formatted_record)
else:
for record in result:
formatted_record = ','.join(str(value) for value in record)
print(formatted_record)