-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
58 lines (47 loc) · 2.34 KB
/
database.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
from sqlalchemy import create_engine, text
from DBconf import dbname, host, password, userName
engine = create_engine(f"mysql+pymysql://{userName}:{password}@{host}/{dbname}?charset=utf8mb4")
def load_jobs_from_db():
with engine.connect() as conn:
result = conn.execute(text("select * from jobs"))
resultList = result.fetchall()
jobsList = []
for row in resultList:
row_dict = row._asdict() # Convert the row to a dictionary
jobsList.append(row_dict)
return jobsList
def load_job_from_db(id):
with engine.connect() as conn:
result = conn.execute(text(f"select * from jobs where id = {id}"))
job = result.fetchall()
if len(job) ==0:
return None
else:
return job[0]._asdict()
# This aproach is likely the must efficiante, but it didn't work
# def all_application_to_db(job, application):
# with engine.connect() as conn:
# query = text("insert into application (job_id, full_name, email, linkedin_url, education, experience, resume_url) values (?, ?, ?, ?, ?, ?, ?)")
# conn.execute(query,{
# "job_id": job['id'],
# "full_name" : application['full_name'],
# "email": application['email'],
# "linkedin_url" : application['linkedin_url'],
# "education" : application['education'],
# "working_experience" : application['working_experience'],
# "resume_url" : application['resume_url']
# }
# )
# This is another approach using formated string, but it's not secure cause to sql injection.
# def all_application_to_db(job, application):
# job_id=job['id'],
# full_name=application['full_name'],
# email=application['email'],
# linkedin_url=application['linkedin_url'],
# education=application['education'],
# working_experience=application['working_experience'],
# resume_url=application['resume_url']
# with engine.connect() as conn:
# query = text(f"insert into application (job_id, full_name, email, linkedin_url, education, working_experience, resume_url) values ({job_id}, {full_name}, {email}, {linkedin_url}, {education}, {working_experience}, {resume_url})")
# conn.execute(query,
# )