-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
126 lines (108 loc) · 4.51 KB
/
Copy pathdatabase.py
File metadata and controls
126 lines (108 loc) · 4.51 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
import sqlalchemy
from sqlalchemy import create_engine, text
# Define the connection string to your MySQL database
connection_string = "mysql+pymysql://sql7730122:idnTnhBtif@sql7.freesqldatabase.com:3306/sql7730122"
# Create the SQLAlchemy engine
engine = create_engine(connection_string)
def fetch_all_software_data():
try:
with engine.connect() as connection:
# Define and execute the query
query = text("SELECT * FROM software;")
result = connection.execute(query).mappings() # Use .mappings() to return as dictionaries
# Convert the result into a list of dictionaries
rows = [dict(row) for row in result] # Each row is already a dictionary-like object
return rows
except Exception as e:
print(f"An error occurred: {e}")
return []
def fetch_software_data_by_id(id):
try:
with engine.connect() as connection:
# Define and execute the query with parameter binding
query = text("SELECT * FROM software WHERE id = :val")
result = connection.execute(query, {"val": id}).mappings() # Use parameter binding correctly
# Convert result to a list of dictionaries
rows = [dict(row) for row in result]
return rows
except Exception as e:
print(f"An error occurred: {e}")
return []
# Example usage
all_software_data = fetch_all_software_data()
print("All software data:", all_software_data)
software_data = fetch_software_data_by_id(1) # Replace 1 with the desired ID
print("Software data by ID:", software_data)
#new stuff for other databases
def fetch_all_requests():
try:
with engine.connect() as connection:
query = text("SELECT * FROM requests;")
result = connection.execute(query).mappings()
rows = [dict(row) for row in result]
return rows
except Exception as e:
print(f"An error occurred: {e}")
return []
def fetch_request_by_id(request_id):
try:
with engine.connect() as connection:
query = text("SELECT * FROM requests WHERE id = :val")
result = connection.execute(query, {"val": request_id}).mappings()
rows = [dict(row) for row in result]
return rows
except Exception as e:
print(f"An error occurred: {e}")
return []
def insert_software(software_name, license_type, price, approved, comments):
try:
with engine.connect() as connection:
query = text("""
INSERT INTO software (Software_Name, License_Type, Price, Approved, Comments)
VALUES (:software_name, :license_type, :price, :approved, :comments)
""")
connection.execute(query, {
"software_name": software_name,
"license_type": license_type,
"price": float(price),
"approved": approved,
"comments": comments
})
connection.commit()
except Exception as e:
print(f"An error occurred: {e}")
def move_to_notapproved(request_id, reason):
try:
with engine.connect() as connection:
# Fetch the request data
query = text("SELECT * FROM requests WHERE id = :val")
result = connection.execute(query, {"val": request_id}).mappings()
request_data = [dict(row) for row in result]
if request_data:
request_data = request_data[0]
# Insert into notapproved
insert_query = text("""
INSERT INTO notapproved (application, comments)
VALUES (:application, :comment)
""")
connection.execute(insert_query, {
"application": request_data["application"],
"comment": reason
})
# Delete from request
delete_query = text("DELETE FROM requests WHERE id = :val")
connection.execute(delete_query, {"val": request_id})
connection.commit()
except Exception as e:
print(f"An error occurred: {e}")
def fetch_all_notapproved():
try:
with engine.connect() as connection:
query = text("SELECT * FROM notapproved;")
result = connection.execute(query).mappings()
connection.commit()
rows = [dict(row) for row in result]
return rows
except Exception as e:
print(f"An error occurred: {e}")
return []